2023 - Day 16 (The Floor Will Be Lava)
View puzzle description on AdventOfCode.com
Part 1
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
#!/usr/bin/env python3
#Advent of Code
#2023 Day 16, Part 1
#Solution by James C. (https://github.com/JamesMCo)
import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
def solve(puzzle_input: list[str]) -> int:
class Contraption:
layout: list[str]
width: int
height: int
def __init__(self: "Contraption", layout: list[str]) -> None:
self.layout = layout[:]
self.width = len(self.layout[0])
self.height = len(self.layout)
def in_bounds(self: "Contraption", x: int, y: int) -> bool:
return 0 <= x < self.width and 0 <= y < self.height
def simulate_until_cycle(self: "Contraption", initial_beam: tuple[int, int, int, int]) -> int:
# Refactored to use more efficient cycle detection after
# a conversation with a friend (@Ggrgra on Twitter)
#
# Before, I was using the entire state of all beams to
# detect a cycle, where I only needed to check whether
# a beam had previously been on a given cell facing in
# the same direction, and then just don't bother to
# simulate that beam any further (as it will only ever
# result in duplicated work and results).
beams = [initial_beam]
seen = {initial_beam}
energised = {(initial_beam[0], initial_beam[1])}
while True:
new_beams: set[tuple[int, int, int, int]] = set()
for x, y, dx, dy in beams:
match (self.layout[y][x], (dx, dy)):
case (".", _) | ("|", (0, _)) | ("-", (_, 0)):
if self.in_bounds(x + dx, y + dy) and (x + dx, y + dy, dx, dy) not in seen:
new_beams.add((x + dx, y + dy, dx, dy))
# Reflect up
case ("/", (1, 0)) | ("\\", (-1, 0)):
if self.in_bounds(x, y - 1) and (x, y - 1, 0, -1) not in seen:
new_beams.add((x, y - 1, 0, -1))
# Reflect right
case ("/", (0, -1)) | ("\\", (0, 1)):
if self.in_bounds(x + 1, y) and (x + 1, y, 1, 0) not in seen:
new_beams.add((x + 1, y, 1, 0))
# Reflect down
case ("/", (-1, 0)) | ("\\", (1, 0)):
if self.in_bounds(x, y + 1) and (x, y + 1, 0, 1) not in seen:
new_beams.add((x, y + 1, 0, 1))
# Reflect left
case ("/", (0, 1)) | ("\\", (0, -1)):
if self.in_bounds(x - 1, y) and (x - 1, y, -1, 0) not in seen:
new_beams.add((x - 1, y, -1, 0))
# Split vertically (no-split case handled in first case)
case ("|", _):
if self.in_bounds(x, y - 1) and (x, y - 1, 0, -1) not in seen:
new_beams.add((x, y - 1, 0, -1))
if self.in_bounds(x, y + 1) and (x, y + 1, 0, 1) not in seen:
new_beams.add((x, y + 1, 0, 1))
# Split horizontally (no-split case handled in first case)
case ("-", _):
if self.in_bounds(x - 1, y) and (x - 1, y, -1, 0) not in seen:
new_beams.add((x - 1, y, -1, 0))
if self.in_bounds(x + 1, y) and (x + 1, y, 1, 0) not in seen:
new_beams.add((x + 1, y, 1, 0))
if not new_beams:
return len(energised)
beams = new_beams
for x, y, dx, dy in beams:
energised.add((x, y))
seen.add((x, y, dx, dy))
return Contraption(puzzle_input).simulate_until_cycle((0, 0, 1, 0))
def main() -> tuple[str, int]:
puzzle_input = util.read.as_lines()
return "The number of tiles that end up being energised is {}.", solve(puzzle_input)
class AOC_Tests(unittest.TestCase):
def test_ex1(self):
return self.assertEqual(solve([".|...\\....",
"|.-.\\.....",
".....|-...",
"........|.",
"..........",
".........\\",
"..../.\\\\..",
".-.-/..|..",
".|....-|.\\",
"..//.|...."]), 46)
if __name__ == "__main__":
run(main)
Part 2
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python3
#Advent of Code
#2023 Day 16, Part 2
#Solution by James C. (https://github.com/JamesMCo)
import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
import typing as t
def solve(puzzle_input: list[str]) -> int:
class Contraption:
layout: list[str]
width: int
height: int
def __init__(self: t.Self, layout: list[str]) -> None:
self.layout = layout[:]
self.width = len(self.layout[0])
self.height = len(self.layout)
def in_bounds(self: t.Self, x: int, y: int) -> bool:
return 0 <= x < self.width and 0 <= y < self.height
def simulate_until_cycle(self: t.Self, initial_beam: tuple[int, int, int, int]) -> int:
# Refactored to use more efficient cycle detection after
# a conversation with a friend (@Ggrgra on Twitter)
#
# Before, I was using the entire state of all beams to
# detect a cycle, where I only needed to check whether
# a beam had previously been on a given cell facing in
# the same direction, and then just don't bother to
# simulate that beam any further (as it will only ever
# result in duplicated work and results).
beams = [initial_beam]
seen = {initial_beam}
energised = {(initial_beam[0], initial_beam[1])}
while True:
new_beams: set[tuple[int, int, int, int]] = set()
for x, y, dx, dy in beams:
match (self.layout[y][x], (dx, dy)):
case (".", _) | ("|", (0, _)) | ("-", (_, 0)):
if self.in_bounds(x + dx, y + dy) and (x + dx, y + dy, dx, dy) not in seen:
new_beams.add((x + dx, y + dy, dx, dy))
# Reflect up
case ("/", (1, 0)) | ("\\", (-1, 0)):
if self.in_bounds(x, y - 1) and (x, y - 1, 0, -1) not in seen:
new_beams.add((x, y - 1, 0, -1))
# Reflect right
case ("/", (0, -1)) | ("\\", (0, 1)):
if self.in_bounds(x + 1, y) and (x + 1, y, 1, 0) not in seen:
new_beams.add((x + 1, y, 1, 0))
# Reflect down
case ("/", (-1, 0)) | ("\\", (1, 0)):
if self.in_bounds(x, y + 1) and (x, y + 1, 0, 1) not in seen:
new_beams.add((x, y + 1, 0, 1))
# Reflect left
case ("/", (0, 1)) | ("\\", (0, -1)):
if self.in_bounds(x - 1, y) and (x - 1, y, -1, 0) not in seen:
new_beams.add((x - 1, y, -1, 0))
# Split vertically (no-split case handled in first case)
case ("|", _):
if self.in_bounds(x, y - 1) and (x, y - 1, 0, -1) not in seen:
new_beams.add((x, y - 1, 0, -1))
if self.in_bounds(x, y + 1) and (x, y + 1, 0, 1) not in seen:
new_beams.add((x, y + 1, 0, 1))
# Split horizontally (no-split case handled in first case)
case ("-", _):
if self.in_bounds(x - 1, y) and (x - 1, y, -1, 0) not in seen:
new_beams.add((x - 1, y, -1, 0))
if self.in_bounds(x + 1, y) and (x + 1, y, 1, 0) not in seen:
new_beams.add((x + 1, y, 1, 0))
if not beams:
return len(energised)
beams = new_beams
for x, y, dx, dy in beams:
energised.add((x, y))
seen.add((x, y, dx, dy))
def starting_positions(self: t.Self) -> t.Iterable[tuple[int, int, int, int]]:
for y in range(self.height):
# Right from left edge
yield 0, y, 1, 0
# Left from right edge
yield 0, y, -1, 0
for x in range(self.width):
# Down from top edge
yield x, 0, 0, 1
# Up from bottom edge
yield x, 0, 0, -1
def find_max_energisation(self: t.Self) -> int:
return max(map(self.simulate_until_cycle, self.starting_positions()))
return Contraption(puzzle_input).find_max_energisation()
def main() -> tuple[str, int]:
puzzle_input = util.read.as_lines()
return "The largest number of tiles that can be energised from any configuration is {}.", solve(puzzle_input)
class AOC_Tests(unittest.TestCase):
def test_ex1(self):
return self.assertEqual(solve([".|...\\....",
"|.-.\\.....",
".....|-...",
"........|.",
"..........",
".........\\",
"..../.\\\\..",
".-.-/..|..",
".|....-|.\\",
"..//.|...."]), 51)
if __name__ == "__main__":
run(main)