-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16.py
141 lines (113 loc) · 4.15 KB
/
day16.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
from __future__ import annotations
from lib import Point
from functools import reduce
from itertools import chain
from enum import Enum, auto
from collections import defaultdict
class Direction(Enum):
N = auto()
S = auto()
E = auto()
W = auto()
class Tile(Enum):
Empty = auto()
ForwardSlash = auto()
BackSlash = auto()
Horizontal = auto()
Vertical = auto()
def parse(ch: str) -> Tile:
match ch:
case "/": return Tile.ForwardSlash
case "\\": return Tile.BackSlash
case "-": return Tile.Horizontal
case "|": return Tile.Vertical
case _: return Tile.Empty
class Contraption(dict):
def parse(input: str) -> Contraption:
return Contraption({
Point(x, y): Tile.parse(ch)
for y, line in enumerate(input.splitlines())
for x, ch in enumerate(line)
})
def trace_beam(self, position: Point, direction: Direction) -> set[Point]:
beams = [(position, direction)]
energised = defaultdict(set)
def energise_tile(p: Point, d: Direction) -> bool:
if p in self:
if p in energised:
if d in energised[p]:
return False
else:
energised[p].add(d)
return True
else:
energised[p].add(d)
return True
else:
return False
while beams:
p, d = beams.pop()
while energise_tile(p, d):
if p in self:
match self[p]:
case Tile.Vertical:
if d == Direction.E or d == Direction.W:
beams.append((p, Direction.N))
beams.append((p, Direction.S))
break
case Tile.Horizontal:
if d == Direction.N or d == Direction.S:
beams.append((p, Direction.E))
beams.append((p, Direction.W))
break
case Tile.ForwardSlash:
match d:
case Direction.N: d = Direction.E
case Direction.S: d = Direction.W
case Direction.E: d = Direction.N
case Direction.W: d = Direction.S
case Tile.BackSlash:
match d:
case Direction.N: d = Direction.W
case Direction.S: d = Direction.E
case Direction.E: d = Direction.S
case Direction.W: d = Direction.N
case Tile.Empty:
pass
match d:
case Direction.N: p += Point(0, -1)
case Direction.S: p += Point(0, 1)
case Direction.E: p += Point(1, 0)
case Direction.W: p += Point(-1, 0)
else:
break
return energised
def part1(input: str) -> int:
return len(Contraption.parse(input).trace_beam(Point(0, 0), Direction.E))
def part2(input: str) -> int:
c = Contraption.parse(input)
max_x, max_y = reduce(lambda acc, p: (max(acc[0], p.x), max(acc[1], p.y)), c.keys(), (0, 0))
def _solve(tup: tuple[Point, Direction]) -> int:
return len(c.trace_beam(*tup))
return max(map(_solve, chain(
map(lambda x: (Point(x, 0), Direction.S), range(max_x + 1)),
map(lambda x: (Point(x, max_y), Direction.N), range(max_x + 1)),
map(lambda y: (Point(0, y), Direction.E), range(max_y + 1)),
map(lambda y: (Point(max_x, y), Direction.W), range(max_y + 1)),
)))
TEST_INPUT = r""".|...\....
|.-.\.....
.....|-...
........|.
..........
.........\
..../.\\..
.-.-/..|..
.|....-|.\
..//.|...."""
PART1_TESTS = [
(TEST_INPUT, 46),
]
PART2_TESTS = [
(TEST_INPUT, 51),
]