Skip to content

Commit 9c40bc4

Browse files
committed
day 16
1 parent 4c86fe8 commit 9c40bc4

File tree

4 files changed

+174
-1
lines changed

4 files changed

+174
-1
lines changed

2023/input/day16.txt

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
\..|...................|........\..............\.................-......./........./...\..........-......./...
2+
........../...-/......\.|............../.................\...........\........-.........|.....\.-.|...........
3+
...............-...........|......|...|......../.......\............../.................................../...
4+
..................................|............../..|.|.../............./..\\........|...|..............\..|..
5+
...........................-...-...-.......................-.....-.-....\.......-............................/
6+
........-...|.......|./../......................./.........................\.................../.....\........
7+
\..\............-.\.........|../................................\....\......................./.............-..
8+
|\...|....\................\...|........\-..............|............\.........\.\..|.|.......................
9+
.....\.........\......|...-...............................-.............-../.../..--......\.....\.............
10+
................|...........................|..........\...........././.................../............../....
11+
.............................\......\.../..|................---................-......\....|...-............-.
12+
..........|........./......\|...|.......|........\......\....-....\...-...../................/..-..../......-.
13+
...|............./.................\../....|...\../.................|...|..|......|...........................
14+
..................................-......../.....|............................................................
15+
...................-.............|...-.........../...........................-..............-...............|.
16+
..-.............../.............................-......-.....-.............|-...../....................../....
17+
....||...../.........................................-...........................................--./-.......\
18+
...................\..........-......./..............|\........................../.-.|..............-..-/...|.
19+
.-............-............../..-........../.......................\...-...||........./.-.....-....../........
20+
.......-.......-........................................../\\.......|......./../..\........\..-.........../../
21+
....../../.......................\.........\-....\.|..................\......................./...-\..........
22+
\.......-........\...-.....................\....-........./-...|......|.......-........................\...\..
23+
...|\.................................../..../..........|......-......../-..|......./..........\../..|......-\
24+
..........\...........................|..-........-/-......../....|...............................\.....|.....
25+
..............\......-............................-.../........|........................./........-...........
26+
.............-..\.//.....................-....-......|....../....-............................/..............|
27+
\........../....|................./.....\......\.../.................|........\............../................
28+
.....-........-/|.........\.....-./............|...\.|......|.......|...................\\.....\.............-
29+
................|.-...-.....................|..........-....\....\......../...............................././
30+
...........|.......\..............\......................................./........-..........................
31+
...........................-..../............|............................|...........\.......................
32+
....................|..........|..................../...........\.....|.........-............\.\...-..\.......
33+
................................-......-......\.....-/......\...\................/................-...........
34+
..|.....\..\......|../....................................................\\......-...../..../..............//
35+
.........\.................\..\..-.-..................\.....\..\.....\..........-.............................
36+
...................\............/..\............|.............../..\............................|\.\...../....
37+
......|...............\.......|......|/........|....-.......|...........-.../.\....\.....|./.......|..........
38+
....\/|................-.-..........|.................../..............|../-...|/..........|..................
39+
..|........./-\.............|...|............|......\...................\..../.........|...............|.-....
40+
...................-................\....|/....-...|......./....../..........-|............/......\../..\\....
41+
...........................-..........................................-...|.|................\../...........-.
42+
......../.....|.........-.......|........./-............|../.......|......-.-.-.......................-..|-...
43+
.........|-...............|............./.....|........../..|........\..........|/.............../-...........
44+
...\.....|..|...........\....-...\/.........................................|...................\......-.-....
45+
.../...-...|.............|.....\...|.................................../..............\.................../...
46+
..........-.........../....|.....|..-.........../.................................-..................\....\/..
47+
....../..............\../......../.....-./....|../.\....-.................../............................../.\
48+
......-.|..........................\...............|.......-...-....../.............//...\|................/..
49+
................/.........|........................|......./..|......../-...........................\....|....
50+
........../...|.........\...........-./......\\...........\......|-......|...\...............\................
51+
../.........................|.................-/...........--.........\.../........................\.........\
52+
........\..|...|.............../...................-.........|.................\..|......./.........-.....-|..
53+
\.../.......|.............|-.../.................\...\..........\...|..........\....../......-................
54+
............|.|....\..--............-/-..................../........................-../..\..-................
55+
.....................................|.................../.................................|..../..-......\...
56+
....|..............|..-.....|../.........................................|........\.................|.........
57+
..............\..|...............\....||........../................................\................./.\......
58+
.....................-../......./....../.................................-............\........../............
59+
.....|.............../......-.\.|........-...................|.....-........\...-.............-....-.......-..
60+
..../.....|..............\../..............................\........|....................|..............\.....
61+
....\................-........\../......|..../......................-.....|............../.../........../.....
62+
....-...........................-.............|...........\..|........|.......\././.....|./..........\........
63+
/....................\..............\.............................-......................../\......../....-...
64+
......\..................-............................................./.\...../.............\....-....\.\....
65+
..................................\-............................/.......|........................-|...........
66+
.................../.../.......|./............\.........\....-.-....-../\.........|........./...\.............
67+
......./.../....|...../|......................|.....|.....|.........|...........\......-.\..................|.
68+
..././...........................|.......\.|......-........./.................................................
69+
........|.|.......................-....................././.......||...........|/....-....\.......|......\/...
70+
.\.\.....|....\......-....|...|.....//........|..-.........-.................-........./......................
71+
.|.....................\............-.................................|..../.............-....../.........../.
72+
.|..\..../...............\..................../...\............................-....../................../.|..
73+
./.\............................/.............-...../-................\.........\.............|...../.........
74+
..........\...............................\............../.........\.....-/........./|........................
75+
.............\..|....|..................................\.........|./......................\.........-........
76+
../.....|-............|...\................|...-....-........./........|.-..........\.........................
77+
........-............./...........--................../...............................|..|.\....\.............
78+
\.............-\.........................-|......|/.........\............/...........-.../.\........\..\.../..
79+
......................|........\....-../.............|......|.-...................................-|../...-...
80+
.............|...|.....-...........|.....-...............|...\........................./..\....\..............
81+
..............|.............-.........../....-..................................|......./........|............
82+
...........|.-..../......................................\....-...\-......../........-..........-......-.....|
83+
../...........-............-.../|...........-.-.............\.............................|.............-.....
84+
.........../...-............../........./...................................../......-.......\\...............
85+
........................./.||......|...../.......|....|..................-..........|......\..................
86+
............/....-./..-...........|...-..........\....../...\/.......|\.....\....|........../..\.\............
87+
.......................................-.../.......|............|........|...\..........\.....-...............
88+
/..\|.............-...-.................................\........\...................\......|....-............
89+
..................\............../........................./.........|..........\...\......\..........|......|
90+
.../.................................................../...|............../....\...\...../........|.......-...
91+
..................-.-..........................\.................-......................-..\.........-........
92+
...................../\.....\.............\.............../................\.|............../........./.......
93+
....................|../.\./......................................-.../.......\...............................
94+
|........................-..............................................|...............................-.....
95+
....\.\...........\......|.........\/|..|....|.....\............-...../.............................\\........
96+
......\....|......\|.....-..................\........\.......................-............-.|..............|..
97+
....|.....././.......................|......../...........-.|............./......-.|....../.....\......|....\.
98+
.........\........|.......\..........|........./..................\.........................|................-
99+
.....\..........\.....-......../...|-........../...\/|........................................................
100+
.../.....................\../.........\.|.........-..........\...............................|...............\
101+
.........-...........................................-.|...........\......//............../..|.\............/.
102+
.-|../...../.........|............./..........................|..-.\......\............\................../...
103+
../....../.................|..............\.........|.\......\...../.......\............\./.......|....../..\.
104+
......../.-..-......\.........................\|...................................-....../...................
105+
......................-....|........./\................|......\..-\....../.......\.................\/.-\......
106+
............\...|...............|............./...........................................\....-.-............
107+
\.........|.............................|............/.........-.....\........\|...-..-..\/.../......./.......
108+
.............................\.|...............\....\..\-.....................\......-...../......\..../.....\
109+
..\-......\.\........-........../........\...............\..................../....-....\.\.|.|...............
110+
................|./........../.................................-.............|...................\............

2023/input/day16_test.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.|...\....
2+
|.-.\.....
3+
.....|-...
4+
........|.
5+
..........
6+
.........\
7+
..../.\\..
8+
.-.-/..|..
9+
.|....-|.\
10+
..//.|....

2023/js/day16.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { asLines } from './util.js';
2+
3+
const layout = asLines('../input/day16.txt').map(line => line.split(''));
4+
5+
const traceLight = (layout, light, visited) => {
6+
const {x, y, dx, dy} = light;
7+
if (x < 0 || y < 0 || x >= layout[0].length || y >= layout.length) {
8+
return []; // light has left the building
9+
}
10+
if (!visited.has(`${x},${y}`)) visited.set(`${x},${y}`, new Set());
11+
if (visited.get(`${x},${y}`).has(`${dx},${dy}`)) return []; // light has looped
12+
visited.get(`${x},${y}`).add(`${dx},${dy}`);
13+
14+
const current = layout[y][x];
15+
let next = [{dx, dy}];
16+
if (current === '/') next = [{dx: -dy, dy: -dx}];
17+
if (current === '\\') next = [{dx: dy, dy: dx}];
18+
if (current === '|' && dx !==0) next = [{dx: dy, dy: -1}, {dx: dy, dy: 1}];
19+
if (current === '-' && dy !==0) next = [{dx: -1, dy: dx}, {dx: 1, dy: dx}];
20+
return next.map(({dx, dy}) => ({x: x + dx, y: y + dy, dx, dy}));
21+
}
22+
23+
const traceLights = (layout, lights) => {
24+
const visited = new Map();
25+
while (lights.length > 0) {
26+
lights = lights.flatMap(light => traceLight(layout, light, visited));
27+
}
28+
return visited.size;
29+
}
30+
31+
const energized = traceLights(layout, [{x: 0, y: 0, dx: 1, dy: 0}]);
32+
console.log(`part1: ${energized}`);
33+
34+
let energized2 = 0;
35+
for (let y = 0; y < layout.length; y++) {
36+
for (let x = 0; x < layout[0].length; x++) {
37+
if (y === 0) {
38+
energized2 = Math.max(energized2, traceLights(layout, [{x, y, dx: 0, dy: 1}]));
39+
}
40+
if (x === 0) {
41+
energized2 = Math.max(energized2, traceLights(layout, [{x, y, dx: 1, dy: 0}]));
42+
}
43+
if (y === layout.length - 1) {
44+
energized2 = Math.max(energized2, traceLights(layout, [{x, y, dx: 0, dy: -1}]));
45+
}
46+
if (x === layout[0].length - 1) {
47+
energized2 = Math.max(energized2, traceLights(layout, [{x, y, dx: -1, dy: 0}]));
48+
}
49+
}
50+
}
51+
52+
console.log(`part2: ${energized2}`);

2023/js/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"day12": "node day12.js",
1818
"day13": "node day13.js",
1919
"day14": "node day14.js",
20-
"day15": "node day15.js"
20+
"day15": "node day15.js",
21+
"day16": "node day16.js"
2122
}
2223
}

0 commit comments

Comments
 (0)