-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
222 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.|...\.... | ||
|.-.\..... | ||
.....|-... | ||
........|. | ||
.......... | ||
.........\ | ||
..../.\\.. | ||
.-.-/..|.. | ||
.|....-|.\ | ||
..//.|.... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
\\.........../..............-..../....\.......\.................|....|..........|............-\.....|......... | ||
................................../.......|..\..........-....................\........../../\../.............. | ||
.............................|...|.............\..\.....|.........\....................../............/-...... | ||
\................\.............|..../........\\.....\\........-.........../..\.../...\.......\/.......|..|.... | ||
......\..............|...|..../........-../.................../....|./................./.\........\.|-........ | ||
.............................-....................-............/.\............../........\/........|...\...... | ||
./........|................\.|........\.......\../.-.........\..-.............|............................... | ||
|....................|...............-/............-.|..|...........................|..................\...... | ||
................../........\../...........|..................\................./.......\...-......../|........ | ||
././...........-..|....................................-.....................|./........../..|.-..\..|.....|.. | ||
.............|.......................||................................../..-........./.................../... | ||
........-......\............/.....-...\..|...........\..........-.........................\................... | ||
\/...............\.|......................\......\.........\/..........\................./.-.../...-.....|.... | ||
....\........................................................\.......-/.............\/.........\.............. | ||
\........\.........\..\..|....\..............-...../.................|.....|.................\.-/............. | ||
.............................\/..-..............\....-...-............................................./...... | ||
....|..../...................//.........\.............\......|...|....../............|.......-.|.-.-|.......\. | ||
.........|..-.-........\\../...........|.....\............................../....\\..\......../......\\..|.... | ||
.......|....................-...././...../.|../........................./...-\...|.......|.......-....|......\ | ||
../.../........-......................|......-.....\..............-../.......|./.......|.............|.......| | ||
........................................................./............-......\.../........\../../.........\... | ||
........................./../.../............-.\.........................|..............|....................| | ||
..-......./.--.......|.............................................\........\.........\...............\......| | ||
.|........................................|..........-....\.......\.......|................................../ | ||
..............................................\.....\........|.....\./......\................|......../...../. | ||
./.\......../..\\..|....-.-|..-.............................................../.\|/........\.........-|...|... | ||
........|........-.\/................-.........-............../.|..........\./|...--...............|.......... | ||
.................-............./././...........|.|.....|....|......\-....|...............|.................... | ||
.......././..............................................|/...\........../......./..||.\..-................... | ||
............-..|....................\..-./.........\.......................................................... | ||
./.-...............\......\...|......\....../..........................-....................-...|......\...... | ||
.....././...-........................\..../..|-.............../.......................................\|...... | ||
-.........................................../...............-....../.....|.........\..-.........|............. | ||
....|.......\....\|.....\.|....|.............\..\...-............../.....................\....-............... | ||
.............-..-..................\..............|..|........................-..........\../.\......../...... | ||
.....|.....................\.....\.......\...........\....|.........\.......//.................|........./.... | ||
|.....\-/.......|............../.....\..........|......./......./...............\./..........|................ | ||
............../|.......................|....../..............-...................-...................../...... | ||
../|....\.......\...............\................-........\..../../.................\/.........\....\......... | ||
........-.....|.|......-...........\............|...................\...........|.......................|..... | ||
.....|....|..\..........................|................\..../....-..................................|....... | ||
..-........||/................/........|......................-....-..................../..|...../............ | ||
............./.............\..-.......................\..................||.-.-..|...............\...|...\.... | ||
............................\....\....../....\...........-...................|....-.....|..................... | ||
.../...........................-..|.....|../.........\./.................\|..............||....|..........\... | ||
.................../........\...........\........../............|...|\....\........|......-...\.......|./...\. | ||
-....|...........\............./..\...........||...................|.............../...............\.......... | ||
.........\.........-..................../.../.......//.............-/...-.-|....\............................. | ||
..-.........\......././................-.........|-...../............|....-.............\..................... | ||
......../.......|.............................\....|......................../................................- | ||
..\........./.--................/................................................................../......-... | ||
.../................|.\....-.................................\.../....................................\....... | ||
.............................\....../.|...........-............................................/....-......... | ||
...\./..........-........./../...............................-..-........-....../..................-.....\.... | ||
.........\........./\........................-.-................................|............................. | ||
..../.|-.................................-............\..................|.........|......./......-........... | ||
.-|..........................-........|...//..\.../.........-............/|.............|..............-..\... | ||
..../......|...\..........|...........-.||....-..|.........-......../..........|................../........... | ||
\.../../............./../...........\...........\.......................................\./.............|..... | ||
.....|.../....|..............................\........\...................|.........|......................... | ||
..........|............|............./......../.../................................-.....-.........../..|..... | ||
......-.\.........................|...../.....\.....-/....-..|.\.........\|....-.........||...........\....... | ||
..\......\............./...............\-............/...|................................/................... | ||
.......|.|...|.|.........|..|.......-...\.........|./..|..................-................|..........-....... | ||
..-\...--..-........|......................-.....|./..............|/.................../...........\....-.../. | ||
...........-...-........-..........-...........|.......-................\.........|............-.............| | ||
.....|.\..-../....\.................../-....\.............|......../....\/.\..\...//..........|............... | ||
......-.-.|..|............................./.........-....|.......|......|..............................-...-. | ||
..|............\...|................./\...|........../...-.|.........\..../.........|-...|.......\...........- | ||
...................\..../..............|...../.....\....../.................\..|.....................-........ | ||
.|............-................../........-.....|..../..........................|../..\.....\....|............ | ||
.............................................|..........|......-\.............-..-........................\... | ||
............./.-....|......|................................................-/|/...|../...........-...|....... | ||
......-................-..-...............|................-.-.............\........|...-./................-.. | ||
...........|...........-/........../..................../|.|...................|...................../........ | ||
.....................................\......................../...........|-...........-................../... | ||
...|.....\.....-...../..............|.........\....\............-\............\/.........................-.... | ||
..............-\......-.............../...|.........................................|.....\/.................. | ||
.................|...|.....|.............../....\...............-....\..........|.-........|........../...|... | ||
..|...-.........\....-.....\.....................|...|.......-....\....-..-......-.\../........-../........... | ||
.......\.....\................\......\-................................|..\.....//.........../..........\..... | ||
........................../.-.................................\--......../..-................................. | ||
.................|............|\./....|.|..|.............//......./.......................|.....\.....\\...... | ||
./...|\...................................../.......................\.........|..............|....|....\...... | ||
......|.............................................................\..|.................\-..../......./...... | ||
.....-.......|........../......./..\...........|...........-..|......../...../................\......\........ | ||
............\....-........\......./.......-...........................................\........-/./........... | ||
|..................................-.........\/..-....././.....\.............../-....................\........ | ||
../.........-..........-....-...-.........\/....................../.../.....\............................-.\.| | ||
........../...-..-........................-........................................./....\.....-.............. | ||
..|-.\........\....................\.\..........-....|.-.-...........\..............\..-.....|................ | ||
..-.../......./\.\...\\.//.....|..........................\........|.-||......................|.......\/...... | ||
...-........-/.............................-......................-|....\...|..../..|.........\............... | ||
.........|..|/........./.......\....\../\.-..-...\..|...../...........\.............../..................../\. | ||
.........\.-\|..|../...........--..........|-......|...|.................||................................... | ||
..................\.....\...........................|.............\..|.........................|...........\.. | ||
....|../........../.............................|./........\\.....|.....|...................................|. | ||
-..........................................-........................-...\..................\.....\......-..... | ||
..\.\......|......|........|.............-|....-.....-...........|../.-.........|........\..\...-...\........\ | ||
.|.......-..|....|..............|.................../...............|.............................-.........// | ||
....................\..\.||./\.......-.........-..-................../..../....................-/..........\.. | ||
.........\.........|...-.....\..........\.\....|.|..............|..........................................\.| | ||
...|\..|................-/..........|\....\/|..-...........................................\.................. | ||
.../...\.|..............................\.-...\.........\....../../...............-./............/............ | ||
.....-.\..............................................\.|....................\..........|...............|..... | ||
.....|................../|....-....-....\..../...............\.....|..|......./.................../........\.. | ||
.......................\....................-.......-.................../..............-...\..\........|...... | ||
...../................\........../..|.../...........|........|............\........-....-/.....\....|......... | ||
.\\.\............|..................................................|......\/..........................\...... | ||
....|........../...../...-.................|......|..........-....................................../.../..... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters