7 день

This commit is contained in:
2025-12-18 14:44:33 +03:00
parent 94f62c2fba
commit 70f0364674
4 changed files with 259 additions and 2 deletions

55
2025/day07/part2.py Normal file
View File

@@ -0,0 +1,55 @@
# 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))