From d4dece6de1eabe085c6297f89c2a0b7cd3d517ad Mon Sep 17 00:00:00 2001 From: AlexAegis Date: Sat, 16 Dec 2023 07:15:26 +0100 Subject: [PATCH] feat: solved 2023 day 16 --- .github/badges/typescript/2023.json | 2 +- resources/2023/16/example.1.txt | 10 ++ resources/2023/16/input.txt | 110 ++++++++++++++++++ .../typescript/2023/16/src/internal/beam.ts | 77 ++++++++++++ solutions/typescript/2023/16/src/p1.spec.ts | 4 +- solutions/typescript/2023/16/src/p1.ts | 10 +- solutions/typescript/2023/16/src/p2.spec.ts | 4 +- solutions/typescript/2023/16/src/p2.ts | 17 ++- solutions/typescript/readme.md | 2 +- 9 files changed, 222 insertions(+), 14 deletions(-) create mode 100644 resources/2023/16/example.1.txt create mode 100644 resources/2023/16/input.txt create mode 100644 solutions/typescript/2023/16/src/internal/beam.ts diff --git a/.github/badges/typescript/2023.json b/.github/badges/typescript/2023.json index 7dc272605..dd2870b95 100644 --- a/.github/badges/typescript/2023.json +++ b/.github/badges/typescript/2023.json @@ -1,6 +1,6 @@ { "schemaVersion": 1, "label": "Advent of TypeScript 2023", - "message": "13/25", + "message": "14/25", "color": "orange" } diff --git a/resources/2023/16/example.1.txt b/resources/2023/16/example.1.txt new file mode 100644 index 000000000..d6805ce6e --- /dev/null +++ b/resources/2023/16/example.1.txt @@ -0,0 +1,10 @@ +.|...\.... +|.-.\..... +.....|-... +........|. +.......... +.........\ +..../.\\.. +.-.-/..|.. +.|....-|.\ +..//.|.... diff --git a/resources/2023/16/input.txt b/resources/2023/16/input.txt new file mode 100644 index 000000000..29c687a86 --- /dev/null +++ b/resources/2023/16/input.txt @@ -0,0 +1,110 @@ +\\.........../..............-..../....\.......\.................|....|..........|............-\.....|......... +................................../.......|..\..........-....................\........../../\../.............. +.............................|...|.............\..\.....|.........\....................../............/-...... +\................\.............|..../........\\.....\\........-.........../..\.../...\.......\/.......|..|.... +......\..............|...|..../........-../.................../....|./................./.\........\.|-........ +.............................-....................-............/.\............../........\/........|...\...... +./........|................\.|........\.......\../.-.........\..-.............|............................... +|....................|...............-/............-.|..|...........................|..................\...... +................../........\../...........|..................\................./.......\...-......../|........ +././...........-..|....................................-.....................|./........../..|.-..\..|.....|.. +.............|.......................||................................../..-........./.................../... +........-......\............/.....-...\..|...........\..........-.........................\................... +\/...............\.|......................\......\.........\/..........\................./.-.../...-.....|.... +....\........................................................\.......-/.............\/.........\.............. +\........\.........\..\..|....\..............-...../.................|.....|.................\.-/............. +.............................\/..-..............\....-...-............................................./...... +....|..../...................//.........\.............\......|...|....../............|.......-.|.-.-|.......\. +.........|..-.-........\\../...........|.....\............................../....\\..\......../......\\..|.... +.......|....................-...././...../.|../........................./...-\...|.......|.......-....|......\ +../.../........-......................|......-.....\..............-../.......|./.......|.............|.......| +........................................................./............-......\.../........\../../.........\... +........................./../.../............-.\.........................|..............|....................| +..-......./.--.......|.............................................\........\.........\...............\......| +.|........................................|..........-....\.......\.......|................................../ +..............................................\.....\........|.....\./......\................|......../...../. +./.\......../..\\..|....-.-|..-.............................................../.\|/........\.........-|...|... +........|........-.\/................-.........-............../.|..........\./|...--...............|.......... +.................-............./././...........|.|.....|....|......\-....|...............|.................... +.......././..............................................|/...\........../......./..||.\..-................... +............-..|....................\..-./.........\.......................................................... +./.-...............\......\...|......\....../..........................-....................-...|......\...... +.....././...-........................\..../..|-.............../.......................................\|...... +-.........................................../...............-....../.....|.........\..-.........|............. +....|.......\....\|.....\.|....|.............\..\...-............../.....................\....-............... +.............-..-..................\..............|..|........................-..........\../.\......../...... +.....|.....................\.....\.......\...........\....|.........\.......//.................|........./.... +|.....\-/.......|............../.....\..........|......./......./...............\./..........|................ +............../|.......................|....../..............-...................-...................../...... +../|....\.......\...............\................-........\..../../.................\/.........\....\......... +........-.....|.|......-...........\............|...................\...........|.......................|..... +.....|....|..\..........................|................\..../....-..................................|....... +..-........||/................/........|......................-....-..................../..|...../............ +............./.............\..-.......................\..................||.-.-..|...............\...|...\.... +............................\....\....../....\...........-...................|....-.....|..................... +.../...........................-..|.....|../.........\./.................\|..............||....|..........\... +.................../........\...........\........../............|...|\....\........|......-...\.......|./...\. +-....|...........\............./..\...........||...................|.............../...............\.......... +.........\.........-..................../.../.......//.............-/...-.-|....\............................. +..-.........\......././................-.........|-...../............|....-.............\..................... +......../.......|.............................\....|......................../................................- +..\........./.--................/................................................................../......-... +.../................|.\....-.................................\.../....................................\....... +.............................\....../.|...........-............................................/....-......... +...\./..........-........./../...............................-..-........-....../..................-.....\.... +.........\........./\........................-.-................................|............................. +..../.|-.................................-............\..................|.........|......./......-........... +.-|..........................-........|...//..\.../.........-............/|.............|..............-..\... +..../......|...\..........|...........-.||....-..|.........-......../..........|................../........... +\.../../............./../...........\...........\.......................................\./.............|..... +.....|.../....|..............................\........\...................|.........|......................... +..........|............|............./......../.../................................-.....-.........../..|..... +......-.\.........................|...../.....\.....-/....-..|.\.........\|....-.........||...........\....... +..\......\............./...............\-............/...|................................/................... +.......|.|...|.|.........|..|.......-...\.........|./..|..................-................|..........-....... +..-\...--..-........|......................-.....|./..............|/.................../...........\....-.../. +...........-...-........-..........-...........|.......-................\.........|............-.............| +.....|.\..-../....\.................../-....\.............|......../....\/.\..\...//..........|............... +......-.-.|..|............................./.........-....|.......|......|..............................-...-. +..|............\...|................./\...|........../...-.|.........\..../.........|-...|.......\...........- +...................\..../..............|...../.....\....../.................\..|.....................-........ +.|............-................../........-.....|..../..........................|../..\.....\....|............ +.............................................|..........|......-\.............-..-........................\... +............./.-....|......|................................................-/|/...|../...........-...|....... +......-................-..-...............|................-.-.............\........|...-./................-.. +...........|...........-/........../..................../|.|...................|...................../........ +.....................................\......................../...........|-...........-................../... +...|.....\.....-...../..............|.........\....\............-\............\/.........................-.... +..............-\......-.............../...|.........................................|.....\/.................. +.................|...|.....|.............../....\...............-....\..........|.-........|........../...|... +..|...-.........\....-.....\.....................|...|.......-....\....-..-......-.\../........-../........... +.......\.....\................\......\-................................|..\.....//.........../..........\..... +........................../.-.................................\--......../..-................................. +.................|............|\./....|.|..|.............//......./.......................|.....\.....\\...... +./...|\...................................../.......................\.........|..............|....|....\...... +......|.............................................................\..|.................\-..../......./...... +.....-.......|........../......./..\...........|...........-..|......../...../................\......\........ +............\....-........\......./.......-...........................................\........-/./........... +|..................................-.........\/..-....././.....\.............../-....................\........ +../.........-..........-....-...-.........\/....................../.../.....\............................-.\.| +........../...-..-........................-........................................./....\.....-.............. +..|-.\........\....................\.\..........-....|.-.-...........\..............\..-.....|................ +..-.../......./\.\...\\.//.....|..........................\........|.-||......................|.......\/...... +...-........-/.............................-......................-|....\...|..../..|.........\............... +.........|..|/........./.......\....\../\.-..-...\..|...../...........\.............../..................../\. +.........\.-\|..|../...........--..........|-......|...|.................||................................... +..................\.....\...........................|.............\..|.........................|...........\.. +....|../........../.............................|./........\\.....|.....|...................................|. +-..........................................-........................-...\..................\.....\......-..... +..\.\......|......|........|.............-|....-.....-...........|../.-.........|........\..\...-...\........\ +.|.......-..|....|..............|.................../...............|.............................-.........// +....................\..\.||./\.......-.........-..-................../..../....................-/..........\.. +.........\.........|...-.....\..........\.\....|.|..............|..........................................\.| +...|\..|................-/..........|\....\/|..-...........................................\.................. +.../...\.|..............................\.-...\.........\....../../...............-./............/............ +.....-.\..............................................\.|....................\..........|...............|..... +.....|................../|....-....-....\..../...............\.....|..|......./.................../........\.. +.......................\....................-.......-.................../..............-...\..\........|...... +...../................\........../..|.../...........|........|............\........-....-/.....\....|......... +.\\.\............|..................................................|......\/..........................\...... +....|........../...../...-.................|......|..........-....................................../.../..... diff --git a/solutions/typescript/2023/16/src/internal/beam.ts b/solutions/typescript/2023/16/src/internal/beam.ts new file mode 100644 index 000000000..d58bc6f6f --- /dev/null +++ b/solutions/typescript/2023/16/src/internal/beam.ts @@ -0,0 +1,77 @@ +import { Direction, GridGraph, Vec2 } from '@alexaegis/advent-of-code-lib'; + +export const mirrors: Record> = { + '\\': { + [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> = { + '|': { + [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>(); + 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; +}; diff --git a/solutions/typescript/2023/16/src/p1.spec.ts b/solutions/typescript/2023/16/src/p1.spec.ts index f417ca27d..1d0901f2b 100644 --- a/solutions/typescript/2023/16/src/p1.spec.ts +++ b/solutions/typescript/2023/16/src/p1.spec.ts @@ -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); }); }); }); diff --git a/solutions/typescript/2023/16/src/p1.ts b/solutions/typescript/2023/16/src/p1.ts index d508a960e..31c092b16 100644 --- a/solutions/typescript/2023/16/src/p1.ts +++ b/solutions/typescript/2023/16/src/p1.ts @@ -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(); + 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 diff --git a/solutions/typescript/2023/16/src/p2.spec.ts b/solutions/typescript/2023/16/src/p2.spec.ts index 687bf4ef6..b80f75446 100644 --- a/solutions/typescript/2023/16/src/p2.spec.ts +++ b/solutions/typescript/2023/16/src/p2.spec.ts @@ -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); }); }); }); diff --git a/solutions/typescript/2023/16/src/p2.ts b/solutions/typescript/2023/16/src/p2.ts index 68c39bd1c..cc4cf4f52 100644 --- a/solutions/typescript/2023/16/src/p2.ts +++ b/solutions/typescript/2023/16/src/p2.ts @@ -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(); + const aabb = gg.boundingBox(); + const startingBeams = [ + ...aabb.getTopEdge().map((position) => ({ position, direction: Direction.SOUTH })), + ...aabb.getRightEdge().map((position) => ({ position, direction: Direction.WEST })), + ...aabb.getBottomEdge().map((position) => ({ position, direction: Direction.NORTH })), + ...aabb.getLeftEdge().map((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 diff --git a/solutions/typescript/readme.md b/solutions/typescript/readme.md index 12f97184b..c8b7df886 100644 --- a/solutions/typescript/readme.md +++ b/solutions/typescript/readme.md @@ -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 | - | - |