File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11# Advent of code Day 07
22# https://adventofcode.com/2025/day/7
33# 07/12/2025
4-
4+
5+
6+ with open ("input.txt" ) as file :
7+ inp = list (map (
8+ lambda s : list (s .strip ()),
9+ file .readlines ()
10+ ))
11+
12+
13+ res1 , res2 = 0 , 1
14+
15+ from copy import deepcopy
16+ rows = deepcopy (inp )
17+
18+ for i , row in enumerate (rows [:- 2 ]):
19+ if i % 2 == 1 : continue
20+ for j , el in enumerate (row ):
21+ if el in ["^" , "." ]: continue
22+ down = rows [i + 2 ][j ]
23+ if down != "^" :
24+ rows [i + 2 ][j ] = "|"
25+ else :
26+ res1 += 1
27+ if j - 1 >= 0 :
28+ rows [i + 2 ][j - 1 ] = "|"
29+
30+ if j + 1 < len (row ):
31+ rows [i + 2 ][j + 1 ] = "|"
32+
33+
34+ nRows = len (inp )
35+ nCols = len (inp [0 ])
36+
37+ # dp[i][j] = number of ways to reach position (i, j)
38+ dp = [[0 ] * nCols for _ in range (nRows )]
39+
40+
41+ startCol = inp [0 ].index ("S" )
42+ dp [0 ][startCol ] = 1 # 1 way to start
43+
44+ for i in range (0 , nRows - 2 , 2 ):
45+ for j in range (nCols ):
46+ if dp [i ][j ] == 0 : # No way to get here
47+ continue
48+
49+ cpt = dp [i ][j ] # Number of ways to get here
50+ downRow = i + 2
51+ down = inp [downRow ][j ]
52+
53+ if down != "^" :
54+ # Just falls
55+ dp [downRow ][j ] += cpt
56+ else :
57+ # Can go left or right
58+ if j > 0 and inp [downRow ][j - 1 ] != "|" :
59+ dp [downRow ][j - 1 ] += cpt
60+
61+ if j + 1 < nCols and inp [downRow ][j + 1 ] != "|" :
62+ dp [downRow ][j + 1 ] += cpt
63+
64+ # Sum all positions in the last row
65+ lastRow = nRows - 2
66+ res2 = sum (dp [lastRow ])
67+
68+ print (res1 , res2 )
You can’t perform that action at this time.
0 commit comments