File tree Expand file tree Collapse file tree 2 files changed +66
-0
lines changed
Expand file tree Collapse file tree 2 files changed +66
-0
lines changed Original file line number Diff line number Diff line change 1+ from collections import defaultdict , deque
2+
3+ with open ("input" ) as f :
4+ ls = f .read ().strip ().split ("\n " )
5+
6+ boardz = {i + 1j * j : x for i , l in enumerate (ls ) for j , x in enumerate (l )}
7+ S = next (z for z , x in boardz .items () if x == "S" )
8+ histories = defaultdict (int , {S : 1 })
9+
10+ beam = deque ([S ])
11+ seen = {S }
12+ splits = 0
13+ while beam :
14+ laser = beam .popleft ()
15+ nxt = laser + 1
16+ if nxt in seen or nxt not in boardz :
17+ continue
18+ seen .add (nxt )
19+ if boardz [nxt ] == "^" :
20+ beam += [nxt - 1j , nxt + 1j ]
21+ histories [nxt - 1j ] += histories [laser ]
22+ histories [nxt + 1j ] += histories [laser ]
23+ splits += 1
24+ else :
25+ beam .append (nxt )
26+ histories [nxt ] += histories [laser ]
27+
28+ # Part 1
29+ print (splits )
30+
31+ # Part 2
32+ max_real = max (z .real for z in boardz )
33+ bottom = [z for z in boardz if z .real == max_real ]
34+ print (sum (histories [b ] for b in bottom ))
Original file line number Diff line number Diff line change 1+ """Solution by treating each splitter individually.
2+
3+ Here, we note that how many beams enter a splitter depends only on
4+ above it, and immediately to its left/right. Moreover, once we find
5+ a splitter directly above a given splitter, we now that we can stop
6+ looking further up that column, since that splitter will block any
7+ beams from higher up.
8+ """
9+
10+ from collections import defaultdict
11+
12+ with open ("input" ) as f :
13+ ls = f .read ().strip ().split ("\n " )
14+
15+ board = {i + 1j * j : x for i , l in enumerate (ls ) for j , x in enumerate (l )}
16+ splitters = [z for z , x in board .items () if x == "^" ]
17+ entering = defaultdict (int , {splitters [0 ]: 1 })
18+
19+ for i in range (1 , len (splitters )):
20+ splitter = splitters [i ]
21+ for j in range (i - 1 , - 1 , - 1 ):
22+ prev = splitters [j ]
23+ if abs (prev .imag - splitter .imag ) == 1 :
24+ entering [splitter ] += entering [prev ]
25+ if prev .imag == splitter .imag :
26+ break
27+
28+ # Part 1
29+ print (sum (b > 0 for b in entering .values ()))
30+
31+ # Part 2
32+ print (sum (entering .values ()) + 1 )
You can’t perform that action at this time.
0 commit comments