99 file .readlines ()
1010 ))
1111
12+ nRows = len (inp )
13+ nCols = len (inp [0 ])
1214
13- res1 , res2 = 0 , 1
14-
15- from copy import deepcopy
16- rows = deepcopy (inp )
17-
18- for i , row in enumerate (rows [:- 2 ]):
15+ res1 = 0
16+ for i , row in enumerate (inp [:- 2 ]):
1917 if i % 2 == 1 : continue
2018 for j , el in enumerate (row ):
2119 if el in ["^" , "." ]: continue
22- down = rows [ i + 2 ][j ]
20+ down = inp [ i + 2 ][j ]
2321 if down != "^" :
24- rows [ i + 2 ][j ] = "|"
22+ inp [ i + 2 ][j ] = "|"
2523 else :
2624 res1 += 1
27- if j - 1 >= 0 :
28- rows [i + 2 ][j - 1 ] = "|"
25+ if j - 1 >= 0 :
26+ inp [i + 2 ][j - 1 ] = "|"
27+
28+ if j + 1 < nCols :
29+ inp [i + 2 ][j + 1 ] = "|"
2930
30- if j + 1 < len (row ):
31- rows [i + 2 ][j + 1 ] = "|"
3231
3332
34- nRows = len (inp )
35- nCols = len (inp [0 ])
3633
3734# dp[i][j] = number of ways to reach position (i, j)
3835dp = [[0 for _ in range (nCols )] for _ in range (nRows )]
4138startCol = inp [0 ].index ("S" )
4239dp [0 ][startCol ] = 1 # 1 way to start
4340
44- for i in range (0 , nRows - 2 , 2 ):
41+ for i in range (0 , nRows - 2 , 2 ):
4542 for j in range (nCols ):
4643 if dp [i ][j ] == 0 : # No way to get here
4744 continue
4845
4946 cpt = dp [i ][j ] # Number of ways to get here
50- downRow = i + 2
47+ downRow = i + 2
5148 down = inp [downRow ][j ]
5249
5350 if down != "^" :
5451 # Just falls
5552 dp [downRow ][j ] += cpt
5653 else :
5754 # Can go left or right
58- if j > 0 and inp [downRow ][j - 1 ] != "|" :
59- dp [downRow ][j - 1 ] += cpt
55+ if j > 0 and inp [downRow ][j - 1 ] != "|" :
56+ dp [downRow ][j - 1 ] += cpt
6057
61- if j + 1 < nCols and inp [downRow ][j + 1 ] != "|" :
62- dp [downRow ][j + 1 ] += cpt
58+ if j + 1 < nCols and inp [downRow ][j + 1 ] != "|" :
59+ dp [downRow ][j + 1 ] += cpt
6360
6461# Sum all positions in the last row
65- lastRow = nRows - 2
62+ lastRow = nRows - 2
6663res2 = sum (dp [lastRow ])
6764
6865print (res1 , res2 )
0 commit comments