File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ from functools import cache , reduce
2+ from os import environ
3+
4+
5+ lines = open ("07.sample.txt" if environ .get ("DEBUG" ) else "07.txt" ).read ().splitlines ()
6+
7+
8+ def part1 (lines ):
9+ def step (state , line ): # reducer a bit forced lol
10+ beams , ans = state
11+ hits = [i for i , c in enumerate (line ) if c == "^" and beams [i ]]
12+ new_beams = beams [:]
13+
14+ for i in hits :
15+ new_beams [i ] = False
16+ new_beams [i - 1 ] = new_beams [i + 1 ] = True
17+
18+ return new_beams , ans + len (hits )
19+
20+ return reduce (step , lines , ([c == "S" for c in lines [0 ]], 0 ))[1 ]
21+
22+
23+ def part2 (lines ):
24+ splitter_rows = [line for line in lines if "^" in line ]
25+
26+ @cache
27+ def dfs (x , c ):
28+ if x == len (splitter_rows ):
29+ return 1
30+ if not (0 <= c < len (lines [0 ])):
31+ return 0
32+ if splitter_rows [x ][c ] == "^" :
33+ return dfs (x + 1 , c - 1 ) + dfs (x + 1 , c + 1 )
34+ return dfs (x + 1 , c )
35+
36+ return dfs (0 , lines [0 ].index ("S" ))
37+
38+
39+ print (part1 (lines ))
40+ print (part2 (lines ))
You can’t perform that action at this time.
0 commit comments