-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js): add solutions for 2023 day 16
- Loading branch information
1 parent
ef03ebb
commit 145b64d
Showing
3 changed files
with
216 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* eslint-disable max-len, no-cond-assign, no-shadow, prefer-const */ | ||
|
||
import { Grid, Orientation, Rotation, dx, dy, isHorizontalOrientation, isVerticalOrientation, solution } from '../../utils'; | ||
|
||
const parse = (input) => Grid.from(input.trim()); | ||
|
||
const energize = (grid, start, orientation = Orientation.RIGHT) => { | ||
const beams = [{ current: start, orientation }]; | ||
|
||
let beam; | ||
while (beam = beams.shift()) { | ||
let { current, orientation } = beam; | ||
while (current) { | ||
current.energized = true; | ||
|
||
let next = null; | ||
if ( | ||
(current.value === '|' && isHorizontalOrientation(orientation)) | ||
|| (current.value === '-' && isVerticalOrientation(orientation)) | ||
) { | ||
// Prevent splitting ad infinitum | ||
if (current.split) break; | ||
current.split = true; | ||
|
||
orientation += Rotation.TURN_LEFT; | ||
next = grid.getPoint(current.x + dx(orientation), current.y + dy(orientation)); | ||
beams.push({ current: next, orientation }); | ||
|
||
orientation += Rotation.TURN_AROUND; | ||
next = grid.getPoint(current.x + dx(orientation), current.y + dy(orientation)); | ||
beams.push({ current: next, orientation }); | ||
break; | ||
} | ||
|
||
if (current.value === '/') { | ||
orientation += isHorizontalOrientation(orientation) ? Rotation.TURN_LEFT : Rotation.TURN_RIGHT; | ||
} else if (current.value === '\\') { | ||
orientation += isHorizontalOrientation(orientation) ? Rotation.TURN_RIGHT : Rotation.TURN_LEFT; | ||
} | ||
next = grid.getPoint(current.x + dx(orientation), current.y + dy(orientation)); | ||
current = next; | ||
} | ||
} | ||
|
||
return grid.filter((point) => point.energized).length; | ||
}; | ||
|
||
const reset = (grid) => { | ||
for (const point of grid) { | ||
point.energized = false; | ||
point.split = false; | ||
} | ||
}; | ||
|
||
export const partOne = solution((input) => { | ||
const grid = parse(input); | ||
const start = grid.getPoint(0, 0); | ||
return energize(grid, start); | ||
}); | ||
|
||
export const partTwo = solution((input) => { | ||
const grid = parse(input); | ||
const { minX, maxX, minY, maxY } = grid; | ||
|
||
const candidates = []; | ||
for (let x = minX; x <= maxX; x++) { | ||
for (let y = minY; y <= maxY; y++) { | ||
const start = grid.getPoint(x, y); | ||
if (x === minX) { | ||
candidates.push({ start, orientation: Orientation.RIGHT }); | ||
} | ||
if (x === maxX) { | ||
candidates.push({ start, orientation: Orientation.LEFT }); | ||
} | ||
if (y === minY) { | ||
candidates.push({ start, orientation: Orientation.DOWN }); | ||
} | ||
if (y === maxY) { | ||
candidates.push({ start, orientation: Orientation.UP }); | ||
} | ||
} | ||
} | ||
|
||
return candidates.reduce((most, candidate) => { | ||
const count = energize(grid, candidate.start, candidate.orientation); | ||
reset(grid); | ||
return count > most ? count : most; | ||
}, -Infinity); | ||
}); |
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,17 @@ | ||
part-one: | ||
- input: &example1 | | ||
.|...\.... | ||
|.-.\..... | ||
.....|-... | ||
........|. | ||
.......... | ||
.........\ | ||
..../.\\.. | ||
.-.-/..|.. | ||
.|....-|.\ | ||
..//.|.... | ||
answer: 46 | ||
|
||
part-two: | ||
- input: *example1 | ||
answer: 51 |
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 @@ | ||
\.......\...-.....|....\....-...../......\\.........../..............\.....\-../....................\......... | ||
.|\......................./.|.\./.|.........-.......\........../.........-........-......................./... | ||
../........\.......|.\...../......-......../.....\............................./................./............ | ||
.\.............................\.../........\.................\....................................-.......... | ||
...............\......|.......-....................|.......|............/-....-................-.............\ | ||
.....\|........|....\.................-../........\./...../.|.....-......................../.............|.... | ||
......-...../...|............|..-...............-...........|....\...................\...............\........ | ||
./......./....|..........-.|.....|....\-...............\......./..............\..|.............|.............. | ||
....|\.......|-....../......\........................................\|......-.................|......|.-..||- | ||
.-.|.......................................\....-./\.-....-.../......|................|.\......./.........-... | ||
.|.../..............|\..................-.....................\..\/|......./..............\...||.|...../..|.|. | ||
....\........./.....\................--...........\........../......\.........................-..-........\... | ||
-.\./.........\...\........./.......-......-...../..-....-..|....././......-........-.-......-.........../.... | ||
.....\.............\............|.\|.........................-/......./...|......|.....\.../.-./.............. | ||
.........-.........\..../.|..................\../........\-............/.-../...\.............-.|.....|....... | ||
......./.|..................../..........-............/....-...................|.............-..........././.- | ||
...-....\...............\...............................|....-.......-.....\.../..|...........\.......-/....|. | ||
........|.....|..../...................|.....\.-..-....-....../..-......\......................|.............. | ||
.......|-.-...-\.......|....|..........|..|..................|..................................|...\../...... | ||
....-|/.../...-..|......-...........\..........-.....||.............../..........\..................|...\..... | ||
..................//...../..........................-..........|...........-.\........./...................... | ||
-..-.................\-.........\.................../...........\../............/...|..............-......|... | ||
..|.|.........|..................../..../-........./.../..........\./...........|............................. | ||
.......|.....\./.....|.-....|.........\.........|......-........./...-...............|.../................./.. | ||
|..............................\................/.|......................./.\...............\........\........ | ||
....\../..........|....../.....|...\.........|-........|....................../............|/.............|... | ||
..\......-........................................\.....|.........-/..\........................../....|....... | ||
.....\-../.....\-/....-..........-.....-.....|.......\.......\....../.../...../....\\......................... | ||
.........../..........-....../......./........-.............\...............\.........-..../...-............-. | ||
........|.....................\..................................-.\.\/.|...../..-..-...\.-.../............... | ||
/...\..........-.....|...................\........./...............-.....|...|......................\......... | ||
................./........\.|..............................|.........../..............\-.........|............ | ||
...\.......\.../...-.../...../|-...|.../.\.\............................|....\.....-.\...../.....\....-...\... | ||
....-....|............/...|...........-.......|.....-....../........................|......|......\....\...... | ||
-.........../\.........|..........|.....\.............-...../........|............../...|................/-.|/ | ||
....................../........\................................|.....\..\..........\..................|...... | ||
..|..............|......../...\......../-.....-........\...................................-.............|.... | ||
.......//....\.........|../...\.......-...-|........\...|..........\........................-...\./....\...... | ||
...-....................|..............\......|......../-...................\.|........\.......-...........\.. | ||
........./..................../\|./..\.......................|./.............................................. | ||
.........../..............\.....................|.......\..............-......-............................... | ||
..........|../................../............................./.../...........\......-.......-.........-..../- | ||
-..........\............|..|.............\................................\......\..-...-..........|/....\..-. | ||
...-...\../............/..|......-.............\./.............../...............\..............-..........|.- | ||
...........|................-..-\..........\|/....-.\.........../.........-.-......................|.......... | ||
\.\.................\..../..........-/..............\.........\.-....\......|........................../../... | ||
..................................-.....|-........|......\...........|..........|.........../.....-..../....-/ | ||
...........-.|..........-..........................|\.\.....|....\....-....................................... | ||
......|.............-.........................../............/../......................|.........-...-........ | ||
......../...-\....\../.......-........|.|......|.......................\.............../...................... | ||
...-................\....\........../..........-\..........................\..-....-.......................... | ||
\........\.............\.......-..-............../......./........-........|...........././............-...... | ||
............./................|.\..........-.....\-....\.........\.............\....../.....\................. | ||
.................../......../................|......\.|...........|.............-./.....|.........../......... | ||
...\/..........\\..............-\...\.....\.....-.............|..........\.......-/........|................-. | ||
....................-...|.......-....-........-..-..................................../................\....-. | ||
.-//................/.....\........\............|............./..................../...................|...... | ||
................../.|............\..-/....-.............-....................../......|..\....../............. | ||
/..\..../.......\-........././.-....\..............\.............-............................-..|............ | ||
........\\........./.\.........../..|....\..........\.........\....../...................\.../-............... | ||
..................................../.........\.../.....|.-....../..../.....|......|.|........../.....\|...\.. | ||
|..|...................\......\.|..............|./.........|.|........-...|...................|............... | ||
-.........|....\........|......................./................\........................\................... | ||
............/................-|-.....|.-..-.................-..........................-............|......... | ||
.....................................\\................|\...\...-....................\........................ | ||
.......-..-................/...................|...../..|................................\......./............ | ||
.............../..................................|.-...............\.../....../.......................-...... | ||
................../.......-...................................-...........\....././...|...........\..../...-.. | ||
......\........\..-.......|.....-...\....................\|................/-...............-.............|.|. | ||
......//.-\.-..|...................|.............|....../..........\.................\\...................-... | ||
.........|......./.|..--.......\.........|..\................|....\........................................... | ||
......................./|..............|.........................\......................\..................... | ||
...-.............\../........|..............\........|.-..................-......................\............ | ||
........\..../......./............-/......|./|..........-...................../........|.-\............../.\.. | ||
.|............................................-../...................\........../..-..|...|..|................ | ||
..........................|..................|....\./.....|...-....-................\..|..\..............\./.. | ||
...........................................|.................|.....\...................|........-......|.-.... | ||
.....\...........|...\...|..........|........./.........\./...............-............................-...... | ||
...|...........\...................\.................-/.........../.......|..../...../..............|.|....... | ||
.|.......|...........\-........\....\......././\......\.........-.......\................\....-............... | ||
../..........................\...........---|....../............-.....................|.......||./..../....... | ||
..../...././...|\................../..........-..../\............//.......\....\.....-.|.................\...- | ||
..............|.............|............\.............|....././....../...../..........|../................... | ||
.|...|...........-..//...............................\................../|....../................../..|....... | ||
........\...\......../......../..../..\....-\.....-/.................................................|...\.../ | ||
...-.../........|./........\........./.....-......................-..-................/......|........\......| | ||
....|..................................|....-.\.........\......-................./..............|............. | ||
...-\.././.\.|...........-..-.........-...............|..-...-....../|.........-......\..../.................. | ||
..................../......../......../........................||...\/...../................/......./...//-... | ||
....-.....................\........-\...\...........|........../.....|.........../......\..................... | ||
..............-.|..../........................\|..............\...........//....\....-....\...........-....... | ||
..........................|...|.....\.......................-..........|...|..........-............-...../.... | ||
.-..../......./|............|.|........../../..\...../....-../\../|.....\.............|\............|......... | ||
........\......./............-..|....|..........|.\.\\..............-............\........./.................\ | ||
.../................-........-.-........\........./.\.|...........|............................/......-....... | ||
.....\....|-..|......\...\.................\..-..........|--\............/..../..\...................-........ | ||
\............\...........|..............................\.|........|.....-.........................\.......... | ||
.............|..-|...................|......\...\-...........|....\....|............././......|\...........\.. | ||
...-....................|...........-........../.......-................./......\.........|.....\.-.........-. | ||
........../......\.\.........|-./.............................|.\......\../..............|...............//... | ||
..|............/..................\..................................|.\.|......................../../..\....\ | ||
....-.-....\\/.......\..........|.-......../................\....................-.............-...../........ | ||
.\.....|.|................\...../...................|..............................\.....|............|..|\... | ||
.-..........|.....\-....................\.......\...................-|/.|........-........\.././..|........... | ||
.....-/.....|.........../........................-..|...........\............|......\..................../.... | ||
../.......-.-/.|..................../....................\.......|.........\........../..\.................... | ||
.....-........-...........................\.............\................../...\.......|\...\.|............... | ||
.........-.....\|.-.........|......\....\..........................-.|..\........|......././....../......|.... | ||
.....................-................../...-...-...................\./......./...-.-....-......./...\..-..... | ||
.\.--./....................-................\...............-..\............-.../...............././.....\.... |