-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday7.py
More file actions
90 lines (65 loc) · 2.04 KB
/
day7.py
File metadata and controls
90 lines (65 loc) · 2.04 KB
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
# Advent of Code 2025, Day 7
# (c) blu3r4y
from collections import defaultdict
from aocd.models import Puzzle
from funcy import print_calls, print_durations
DOWN = 1j
@print_calls
@print_durations(unit="ms")
def part1(data):
start, splitters, limits = data
beams = set()
beams.add(start + DOWN) # initial beam
num_splits = 0
for r in range(2, limits[0] + 1):
splits = {s for s in splitters if s.imag == r}
previous_beams = beams.copy()
beams = set()
for pos in previous_beams:
target = pos + DOWN
if target in splits:
beams.add(target + 1)
beams.add(target - 1)
num_splits += 1
else:
beams.add(target)
return num_splits
@print_calls
@print_durations(unit="ms")
def part2(data):
start, splitters, limits = data
traces = defaultdict(int)
traces[start + DOWN] = 1 # initial beam
for r in range(2, limits[0] + 1):
splits = {s for s in splitters if s.imag == r}
new_traces = defaultdict(int)
for pos, count in traces.items():
target = pos + DOWN
if target in splits:
new_traces[target + 1] += count
new_traces[target - 1] += count
else:
new_traces[target] += count
traces = new_traces
return sum(traces.values())
def load(data):
start = None
splitters = set()
rows = data.splitlines()
for r, row in enumerate(data.splitlines()):
for c, cell in enumerate(row):
pos = complex(c, r)
if cell == "^":
splitters.add(pos)
elif cell == "S":
start = pos
limits = len(rows), len(rows[0])
return start, splitters, limits
if __name__ == "__main__":
puzzle = Puzzle(year=2025, day=7)
ans1 = part1(load(puzzle.input_data))
assert ans1 == 1633
puzzle.answer_a = ans1
ans2 = part2(load(puzzle.input_data))
assert ans2 == 34339203133559
puzzle.answer_b = ans2