Skip to content

Commit be9b671

Browse files
committed
2023: Day 16
1 parent d099b1f commit be9b671

File tree

3 files changed

+203
-0
lines changed

3 files changed

+203
-0
lines changed

2023/python/day16/__init__.py

Whitespace-only changes.

2023/python/day16/__main__.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import queue
2+
from functools import cache
3+
from typing import Iterable
4+
5+
from utils.file import read_input
6+
7+
directions = {
8+
">": (0, 1),
9+
"<": (0, -1),
10+
"^": (-1, 0),
11+
"v": (1, 0),
12+
}
13+
14+
15+
def main() -> None:
16+
lines = read_input(__package__)
17+
height = len(lines)
18+
width = len(lines[0])
19+
20+
def next_beam(x, y, d) -> Iterable[tuple[int, int, str]]:
21+
if lines[x][y] == "/":
22+
if d == ">":
23+
yield x, y, "^"
24+
elif d == "<":
25+
yield x, y, "v"
26+
elif d == "^":
27+
yield x, y, ">"
28+
else:
29+
yield x, y, "<"
30+
elif lines[x][y] == "\\":
31+
if d == ">":
32+
yield x, y, "v"
33+
elif d == "<":
34+
yield x, y, "^"
35+
elif d == "^":
36+
yield x, y, "<"
37+
else:
38+
yield x, y, ">"
39+
elif lines[x][y] == "|":
40+
if d in {">", "<"}:
41+
yield x, y, "^"
42+
yield x, y, "v"
43+
else:
44+
yield x, y, d
45+
elif lines[x][y] == "-":
46+
if d in {"^", "v"}:
47+
yield x, y, "<"
48+
yield x, y, ">"
49+
else:
50+
yield x, y, d
51+
else:
52+
yield x, y, d
53+
54+
def energize(start: tuple[int, int, str]) -> int:
55+
Q: queue.Queue = queue.Queue()
56+
Q.put(start)
57+
visited = set()
58+
59+
while not Q.empty():
60+
x, y, d = Q.get()
61+
visited.add((x, y, d))
62+
63+
dx, dy = directions[d]
64+
x += dx
65+
y += dy
66+
67+
if x < 0 or x >= height or y < 0 or y >= width:
68+
continue
69+
70+
for x, y, d in next_beam(x, y, d):
71+
if (x, y, d) in visited:
72+
continue
73+
Q.put((x, y, d))
74+
75+
energized = set((x, y) for x, y, _ in visited if 0 <= x < height and 0 <= y < width)
76+
return len(energized)
77+
78+
print("Part 1:", energize((0, -1, ">")))
79+
80+
configurations = set()
81+
for i in range(height):
82+
configurations.add((i, -1, ">"))
83+
configurations.add((i, width, "<"))
84+
for i in range(width):
85+
configurations.add((-1, i, "v"))
86+
configurations.add((height, i, "^"))
87+
88+
energized = [(start, energize(start)) for start in configurations]
89+
print("Part 2:", max(energized, key=lambda x: x[1])[1])
90+
91+
92+
if __name__ == "__main__":
93+
main()

2023/python/day16/input.txt

+110
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+
..\....\......./-\.\...........-\................|.........../................||.../.....-...........\........

0 commit comments

Comments
 (0)