Files
advent-of-code/2025/day07/part2.py
2025-12-18 14:44:33 +03:00

55 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# input = """.......S.......
# ...............
# .......^.......
# ...............
# ......^.^......
# ...............
# .....^.^.^.....
# ...............
# ....^.^...^....
# ...............
# ...^.^...^.^...
# ...............
# ..^...^.....^..
# ...............
# .^.^.^.^.^...^.
# ..............."""
with open("input.txt", "r") as file:
input = file.read()
def count_timelines(grid: str) -> int:
lines = grid.splitlines()
H = len(lines)
W = len(lines[0])
# стартовая колонка S
s_col = lines[0].index("S")
counts = [0] * W
counts[s_col] = 1 # одна частица -> один таймлайн в старте
for i in range(1, H):
new = [0] * W
row = lines[i]
for j, cell in enumerate(row):
k = counts[j]
if k == 0:
continue
if cell == "^":
# splitter: поток не проходит вниз, а расходится влево/вправо
if j - 1 >= 0:
new[j - 1] += k
if j + 1 < W:
new[j + 1] += k
else:
# пусто: идем прямо вниз
new[j] += k
counts = new
return sum(counts)
print(count_timelines(input))