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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
# /// script
# requires-python = ">=3.13"
# dependencies = ["pytest>=8"]
# ///
import sys
import time
from enum import StrEnum
from itertools import permutations
from pathlib import Path
INPUT = Path(__file__).parent / "data/inputs/03.txt"
class Field(StrEnum):
TREE = "#"
FREE = "."
type Position = complex
type Distance2D = complex
def create_forest(puzzle: str) -> set[Position]:
return {
complex(x, y)
for y, line in enumerate(puzzle.strip().splitlines())
for x, char in enumerate(line.strip())
if char == "#"
}
def part_one(puzzle: str) -> int:
forest = create_forest(puzzle)
max_x = int(max(p.real for p in forest))
squares = 0
for pos in forest:
for x in range(int(pos.real) + 1, max_x + 1):
if is_aligned_square(forest, pos, x + pos.imag * 1j):
squares += 1
return squares
def is_aligned_square(forest: set[Position], pos_lo: Position, pos_ro: Position) -> bool:
assert pos_lo in forest
distance = pos_ro.real - pos_lo.real
pos_lu = pos_lo + distance * 1j
pos_ru = pos_ro + distance * 1j
return pos_ro in forest and pos_lu in forest and pos_ru in forest
def part_two(puzzle: str) -> int:
forest = create_forest(puzzle)
squares = set[frozenset[Position]]()
for a, b in permutations(forest, 2):
# Now check two squares, as this can now go only in two directions:
dis = b - a
c = a + dis.real * 1j - dis.imag
d = b + dis.real * 1j - dis.imag
if c in forest and d in forest:
squares.add(frozenset((a, b, c, d)))
return len(squares)
def main() -> None:
try:
text = INPUT.read_text()
except FileNotFoundError:
raise SystemExit("no input, run `./aoc get 03`") from None
# `./aoc submit` parses these "Part N: " lines, keep the prefix. Timing goes to stderr.
for n, part in [(1, part_one), (2, part_two)]:
t = time.perf_counter()
print(f"Part {n}: {part(text)}")
print(f" ({time.perf_counter() - t:.2f}s)", file=sys.stderr)
if __name__ == "__main__":
main()
EXAMPLES_1_1 = (
"""
#...#
.....
.....
.....
#...#
""",
"""
.#..#
.....
.....
.#..#
.....
""",
"""
.....
.....
.##..
.##..
.....
""",
"""
#.###
.##.#
##.##
.#.#.
.##.#
""",
)
EXAMPLES_1_2 = (
"""
#..#
.##.
.##.
#..#
""",
"""
#...#
.....
..#.#
.....
#.#.#
""",
"""
#...#..
.......
..#...#
.......
#...#..
.......
..#...#
""",
"""
.##..
.##.#
.....
#.##.
..##.
""",
)
def test_part_one_example_1():
for puzzle in EXAMPLES_1_1:
assert part_one(puzzle) == 1
def test_part_one_example_2():
for puzzle in EXAMPLES_1_2:
assert part_one(puzzle) == 2
def test_part_one_example_5_30():
example = """
#.#.#
.....
#.#.#
.....
#.#.#
"""
assert part_one(example) == 5
example = """
#####
#####
#####
#####
#####
"""
assert part_one(example) == 30
def test_part_one_solution():
text = INPUT.read_text()
assert part_one(text) == 314
def test_part_two_example_1():
examples = """
#...#
.....
.....
.....
#...#
.#...
....#
.....
#....
...#.
..#..
.....
#...#
.....
..#..
....#..
.......
#......
.......
......#
.......
..#....
"""
for example in examples.split("\n\n"):
assert part_two(example.strip()) == 1
def test_part_two_6_1():
example = """
#..#..#
.......
.......
#..#..#
.......
.......
#..#..#
"""
assert part_two(example) == 6
def test_part_two_6_2():
example = """
..#....
....#..
.#....#
...#...
#....#.
..#....
....#..
"""
assert part_two(example) == 6
def test_part_two_solution():
assert part_two(INPUT.read_text().strip()) == 1596
|