55 lines
1.3 KiB
Python
55 lines
1.3 KiB
Python
# 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)) |