Skip to content

Commit

Permalink
feat: solved 2023 day 16
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAegis committed Dec 16, 2023
1 parent ff25740 commit d4dece6
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/badges/typescript/2023.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"schemaVersion": 1,
"label": "Advent of TypeScript 2023",
"message": "13/25",
"message": "14/25",
"color": "orange"
}
10 changes: 10 additions & 0 deletions resources/2023/16/example.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.|...\....
|.-.\.....
.....|-...
........|.
..........
.........\
..../.\\..
.-.-/..|..
.|....-|.\
..//.|....
110 changes: 110 additions & 0 deletions resources/2023/16/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
\\.........../..............-..../....\.......\.................|....|..........|............-\.....|.........
................................../.......|..\..........-....................\........../../\../..............
.............................|...|.............\..\.....|.........\....................../............/-......
\................\.............|..../........\\.....\\........-.........../..\.../...\.......\/.......|..|....
......\..............|...|..../........-../.................../....|./................./.\........\.|-........
.............................-....................-............/.\............../........\/........|...\......
./........|................\.|........\.......\../.-.........\..-.............|...............................
|....................|...............-/............-.|..|...........................|..................\......
................../........\../...........|..................\................./.......\...-......../|........
././...........-..|....................................-.....................|./........../..|.-..\..|.....|..
.............|.......................||................................../..-........./.................../...
........-......\............/.....-...\..|...........\..........-.........................\...................
\/...............\.|......................\......\.........\/..........\................./.-.../...-.....|....
....\........................................................\.......-/.............\/.........\..............
\........\.........\..\..|....\..............-...../.................|.....|.................\.-/.............
.............................\/..-..............\....-...-............................................./......
....|..../...................//.........\.............\......|...|....../............|.......-.|.-.-|.......\.
.........|..-.-........\\../...........|.....\............................../....\\..\......../......\\..|....
.......|....................-...././...../.|../........................./...-\...|.......|.......-....|......\
../.../........-......................|......-.....\..............-../.......|./.......|.............|.......|
........................................................./............-......\.../........\../../.........\...
........................./../.../............-.\.........................|..............|....................|
..-......./.--.......|.............................................\........\.........\...............\......|
.|........................................|..........-....\.......\.......|................................../
..............................................\.....\........|.....\./......\................|......../...../.
./.\......../..\\..|....-.-|..-.............................................../.\|/........\.........-|...|...
........|........-.\/................-.........-............../.|..........\./|...--...............|..........
.................-............./././...........|.|.....|....|......\-....|...............|....................
.......././..............................................|/...\........../......./..||.\..-...................
............-..|....................\..-./.........\..........................................................
./.-...............\......\...|......\....../..........................-....................-...|......\......
.....././...-........................\..../..|-.............../.......................................\|......
-.........................................../...............-....../.....|.........\..-.........|.............
....|.......\....\|.....\.|....|.............\..\...-............../.....................\....-...............
.............-..-..................\..............|..|........................-..........\../.\......../......
.....|.....................\.....\.......\...........\....|.........\.......//.................|........./....
|.....\-/.......|............../.....\..........|......./......./...............\./..........|................
............../|.......................|....../..............-...................-...................../......
../|....\.......\...............\................-........\..../../.................\/.........\....\.........
........-.....|.|......-...........\............|...................\...........|.......................|.....
.....|....|..\..........................|................\..../....-..................................|.......
..-........||/................/........|......................-....-..................../..|...../............
............./.............\..-.......................\..................||.-.-..|...............\...|...\....
............................\....\....../....\...........-...................|....-.....|.....................
.../...........................-..|.....|../.........\./.................\|..............||....|..........\...
.................../........\...........\........../............|...|\....\........|......-...\.......|./...\.
-....|...........\............./..\...........||...................|.............../...............\..........
.........\.........-..................../.../.......//.............-/...-.-|....\.............................
..-.........\......././................-.........|-...../............|....-.............\.....................
......../.......|.............................\....|......................../................................-
..\........./.--................/................................................................../......-...
.../................|.\....-.................................\.../....................................\.......
.............................\....../.|...........-............................................/....-.........
...\./..........-........./../...............................-..-........-....../..................-.....\....
.........\........./\........................-.-................................|.............................
..../.|-.................................-............\..................|.........|......./......-...........
.-|..........................-........|...//..\.../.........-............/|.............|..............-..\...
..../......|...\..........|...........-.||....-..|.........-......../..........|................../...........
\.../../............./../...........\...........\.......................................\./.............|.....
.....|.../....|..............................\........\...................|.........|.........................
..........|............|............./......../.../................................-.....-.........../..|.....
......-.\.........................|...../.....\.....-/....-..|.\.........\|....-.........||...........\.......
..\......\............./...............\-............/...|................................/...................
.......|.|...|.|.........|..|.......-...\.........|./..|..................-................|..........-.......
..-\...--..-........|......................-.....|./..............|/.................../...........\....-.../.
...........-...-........-..........-...........|.......-................\.........|............-.............|
.....|.\..-../....\.................../-....\.............|......../....\/.\..\...//..........|...............
......-.-.|..|............................./.........-....|.......|......|..............................-...-.
..|............\...|................./\...|........../...-.|.........\..../.........|-...|.......\...........-
...................\..../..............|...../.....\....../.................\..|.....................-........
.|............-................../........-.....|..../..........................|../..\.....\....|............
.............................................|..........|......-\.............-..-........................\...
............./.-....|......|................................................-/|/...|../...........-...|.......
......-................-..-...............|................-.-.............\........|...-./................-..
...........|...........-/........../..................../|.|...................|...................../........
.....................................\......................../...........|-...........-................../...
...|.....\.....-...../..............|.........\....\............-\............\/.........................-....
..............-\......-.............../...|.........................................|.....\/..................
.................|...|.....|.............../....\...............-....\..........|.-........|........../...|...
..|...-.........\....-.....\.....................|...|.......-....\....-..-......-.\../........-../...........
.......\.....\................\......\-................................|..\.....//.........../..........\.....
........................../.-.................................\--......../..-.................................
.................|............|\./....|.|..|.............//......./.......................|.....\.....\\......
./...|\...................................../.......................\.........|..............|....|....\......
......|.............................................................\..|.................\-..../......./......
.....-.......|........../......./..\...........|...........-..|......../...../................\......\........
............\....-........\......./.......-...........................................\........-/./...........
|..................................-.........\/..-....././.....\.............../-....................\........
../.........-..........-....-...-.........\/....................../.../.....\............................-.\.|
........../...-..-........................-........................................./....\.....-..............
..|-.\........\....................\.\..........-....|.-.-...........\..............\..-.....|................
..-.../......./\.\...\\.//.....|..........................\........|.-||......................|.......\/......
...-........-/.............................-......................-|....\...|..../..|.........\...............
.........|..|/........./.......\....\../\.-..-...\..|...../...........\.............../..................../\.
.........\.-\|..|../...........--..........|-......|...|.................||...................................
..................\.....\...........................|.............\..|.........................|...........\..
....|../........../.............................|./........\\.....|.....|...................................|.
-..........................................-........................-...\..................\.....\......-.....
..\.\......|......|........|.............-|....-.....-...........|../.-.........|........\..\...-...\........\
.|.......-..|....|..............|.................../...............|.............................-.........//
....................\..\.||./\.......-.........-..-................../..../....................-/..........\..
.........\.........|...-.....\..........\.\....|.|..............|..........................................\.|
...|\..|................-/..........|\....\/|..-...........................................\..................
.../...\.|..............................\.-...\.........\....../../...............-./............/............
.....-.\..............................................\.|....................\..........|...............|.....
.....|................../|....-....-....\..../...............\.....|..|......./.................../........\..
.......................\....................-.......-.................../..............-...\..\........|......
...../................\........../..|.../...........|........|............\........-....-/.....\....|.........
.\\.\............|..................................................|......\/..........................\......
....|........../...../...-.................|......|..........-....................................../.../.....
77 changes: 77 additions & 0 deletions solutions/typescript/2023/16/src/internal/beam.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Direction, GridGraph, Vec2 } from '@alexaegis/advent-of-code-lib';

export const mirrors: Record<string, Record<string, Direction>> = {
'\\': {
[Direction.WEST.toString()]: Direction.SOUTH,
[Direction.NORTH.toString()]: Direction.EAST,
[Direction.EAST.toString()]: Direction.NORTH,
[Direction.SOUTH.toString()]: Direction.WEST,
},
'/': {
[Direction.WEST.toString()]: Direction.NORTH,
[Direction.NORTH.toString()]: Direction.WEST,
[Direction.EAST.toString()]: Direction.SOUTH,
[Direction.SOUTH.toString()]: Direction.EAST,
},
};

export const splitters: Record<string, Record<string, [Direction, Direction] | undefined>> = {
'|': {
[Direction.EAST.toString()]: [Direction.NORTH, Direction.SOUTH],
[Direction.WEST.toString()]: [Direction.SOUTH, Direction.NORTH],
},
'-': {
[Direction.NORTH.toString()]: [Direction.EAST, Direction.WEST],
[Direction.SOUTH.toString()]: [Direction.WEST, Direction.EAST],
},
};

export interface Beam {
position: Vec2;
direction: Direction;
}

export const energizeMap = (gg: GridGraph, startingBeam: Beam): number => {
let beams: Beam[] = [startingBeam];
const beamPathHistory = new Map<string, Set<string>>();
while (beams.length > 0) {
const spawnedBeams: Beam[] = [];
for (const beam of beams) {
const node = gg.getNode(beam.position);
if (node) {
const pathHistory = beamPathHistory.getOrAdd(
node.coordinate.toString(),
() => new Set(),
);

if (pathHistory.has(beam.direction.toString())) {
beam.direction = Direction.ZERO;
} else {
pathHistory.add(beam.direction.toString());
const mirroredTo =
mirrors[node.value.toString()]?.[beam.direction.reverse().toString()];
const splitter =
splitters[node.value.toString()]?.[beam.direction.reverse().toString()];
if (mirroredTo) {
beam.direction = mirroredTo;
beam.position = beam.position.add(mirroredTo);
} else if (splitter) {
const [dirA, dirB] = splitter;
beam.direction = dirA;
beam.position = beam.position.add(dirA);
spawnedBeams.push({ position: beam.position.add(dirB), direction: dirB });
} else {
beam.position = beam.position.add(beam.direction);
}
}
} else {
beam.direction = Direction.ZERO;
}
}
beams.push(...spawnedBeams);
spawnedBeams.clear();
beams = beams.filter((beam) => !beam.direction.equals(Direction.ZERO));
}
// gg.print((n) => (beamPathHistory.has(n.coordinate.toString()) ? '#' : n.toString()));
return beamPathHistory.size;
};
4 changes: 2 additions & 2 deletions solutions/typescript/2023/16/src/p1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ describe('2023 16 p1', () => {
describe('the input', () => {
it('should solve the input', async () => {
const resources = await loadTaskResources(packageJson.aoc);
expect(p1(resources.input)).toEqual(0);
expect(p1(resources.input)).toEqual(8034);
});
});

describe('example 1', () => {
it('should be solved', async () => {
const resources = await loadTaskResources(packageJson.aoc, 'example.1.txt');
expect(p1(resources.input)).toEqual(0);
expect(p1(resources.input)).toEqual(46);
});
});
});
10 changes: 6 additions & 4 deletions solutions/typescript/2023/16/src/p1.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { task } from '@alexaegis/advent-of-code-lib';
import { Direction, Vec2, task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json';
import { energizeMap } from './internal/beam.js';

export const p1 = (_input: string): number => {
return 0;
export const p1 = (input: string): number => {
const gg = input.toGridGraph<string>();
return energizeMap(gg, { position: new Vec2(0, 0), direction: Direction.EAST });
};

await task(p1, packageJson.aoc); // 0 ~0ms
await task(p1, packageJson.aoc); // 8034 ~31.37ms
4 changes: 2 additions & 2 deletions solutions/typescript/2023/16/src/p2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ describe('2023 16 p2', () => {
describe('the input', () => {
it('should solve the input', async () => {
const { input } = await loadTaskResources(packageJson.aoc);
expect(p2(input)).toEqual(0);
expect(p2(input)).toEqual(8225);
});
});

describe('example 1', () => {
it('should be solved', async () => {
const { input } = await loadTaskResources(packageJson.aoc, 'example.1.txt');
expect(p2(input)).toEqual(0);
expect(p2(input)).toEqual(51);
});
});
});
17 changes: 13 additions & 4 deletions solutions/typescript/2023/16/src/p2.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { task } from '@alexaegis/advent-of-code-lib';
import { Direction, task } from '@alexaegis/advent-of-code-lib';
import packageJson from '../package.json';
import { energizeMap, type Beam } from './internal/beam.js';

export const p2 = (_input: string): number => {
return 0;
export const p2 = (input: string): number => {
const gg = input.toGridGraph<string>();
const aabb = gg.boundingBox();
const startingBeams = [
...aabb.getTopEdge().map<Beam>((position) => ({ position, direction: Direction.SOUTH })),
...aabb.getRightEdge().map<Beam>((position) => ({ position, direction: Direction.WEST })),
...aabb.getBottomEdge().map<Beam>((position) => ({ position, direction: Direction.NORTH })),
...aabb.getLeftEdge().map<Beam>((position) => ({ position, direction: Direction.EAST })),
];
return startingBeams.map((beam) => energizeMap(gg, beam)).max();
};

await task(p2, packageJson.aoc); // 0 ~0ms
await task(p2, packageJson.aoc); // 8225 ~1s
2 changes: 1 addition & 1 deletion solutions/typescript/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
| [Day 13](/solutions/typescript/2023/13/) | [?ms](/solutions/typescript/2023/13/src/p1.ts) | [?ms](/solutions/typescript/2023/13/src/p2.ts) |
| [Day 14](/solutions/typescript/2023/14/) | [?ms](/solutions/typescript/2023/14/src/p1.ts) | [?ms](/solutions/typescript/2023/14/src/p2.ts) |
| [Day 15](/solutions/typescript/2023/15/) | [0.19ms](/solutions/typescript/2023/15/src/p1.ts) | [0.65ms](/solutions/typescript/2023/15/src/p2.ts) |
| Day 16 | - | - |
| [Day 16](/solutions/typescript/2023/16/) | [31.37ms](/solutions/typescript/2023/16/src/p1.ts) | [\~1s](/solutions/typescript/2023/16/src/p2.ts) |
| Day 17 | - | - |
| Day 18 | - | - |
| Day 19 | - | - |
Expand Down

0 comments on commit d4dece6

Please sign in to comment.