-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
122 lines (97 loc) · 2.86 KB
/
solution.py
File metadata and controls
122 lines (97 loc) · 2.86 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
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
"""
Advent of Code 2025 - Day 7
"""
def part1(input_data):
"""Solve part 1 of the puzzle."""
start = ()
rows = len(input_data)
cols = len(input_data[0])
# find the start letter 'S'
for r in range(rows):
for c in range(cols):
if input_data[r][c] == 'S':
start = (r, c)
# beams: list of (row, col)
beams = [(start[0] + 1, start[1])] # now start moving downward
visited = set()
splits = 0
while beams:
r, c = beams.pop()
# out of bounds
if not (0 <= r < rows and 0 <= c < cols):
continue
# avoid reprocessing same cell
if (r, c) in visited:
continue
visited.add((r, c))
cell = input_data[r][c]
if cell == '.':
beams.append((r + 1, c))
elif cell == '^':
splits += 1
# spawn left and right beams
beams.append((r, c - 1))
beams.append((r, c + 1))
elif cell == 'S':
beams.append((r + 1, c))
return splits
def part2(input_data):
"""Solve part 2 of the puzzle."""
from collections import deque
rows, cols = len(input_data), len(input_data[0])
# find the start letter 'S'
start = None
for r in range(rows):
for c in range(cols):
if input_data[r][c] == 'S':
start = (r, c)
break
if start:
break
if not start:
return 0
ways = [[0] * cols for _ in range(rows + 1)]
sr, sc = start
seed_row = min(sr + 1, rows)
ways[seed_row][sc] += 1
for r in range(seed_row, rows + 1):
if r == rows:
continue
curr = ways[r][:]
ways[r] = [0] * cols
dq = deque([c for c, v in enumerate(curr) if v > 0])
while dq:
c = dq.popleft()
v = curr[c]
if v == 0:
continue
curr[c] = 0
cell = input_data[r][c]
if cell == '.':
ways[r + 1][c] += v
elif cell == '^':
if c - 1 >= 0:
was = curr[c - 1]
curr[c - 1] += v
if was == 0:
dq.append(c - 1)
if c + 1 < cols:
was = curr[c + 1]
curr[c + 1] += v
if was == 0:
dq.append(c + 1)
elif cell == 'S':
ways[r + 1][c] += v
total = sum(ways[rows][c] for c in range(cols))
return total
def main():
# Read input
with open("day07/input.txt", "r") as f:
input_data = [line.strip() for line in f.readlines() if line.strip()]
# Solve
result1 = part1(input_data)
result2 = part2(input_data)
print(f"Part 1: {result1}")
print(f"Part 2: {result2}")
if __name__ == "__main__":
main()