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+ """Solution by ordinary BFS."""
2+
3+ from collections import defaultdict , deque
4+
5+ with open ("input" ) as f :
6+ ls = f .read ().strip ().split ("\n " )
7+
8+ board = {i + 1j * j : x for i , l in enumerate (ls ) for j , x in enumerate (l )}
9+ S = next (z for z , x in board .items () if x == "S" )
10+ histories = defaultdict (int , {S : 1 })
11+
12+ beam = deque ([S ])
13+ seen = {S }
14+ splits = 0
15+ while beam :
16+ laser = beam .popleft ()
17+ nxt = laser + 1
18+ if nxt in seen or nxt not in board :
19+ continue
20+ seen .add (nxt )
21+ if board [nxt ] == "^" :
22+ beam += [nxt - 1j , nxt + 1j ]
23+ histories [nxt - 1j ] += histories [laser ]
24+ histories [nxt + 1j ] += histories [laser ]
25+ splits += 1
26+ else :
27+ beam .append (nxt )
28+ histories [nxt ] += histories [laser ]
29+
30+ # Part 1
31+ print (splits )
32+
33+ # Part 2
34+ max_real = max (z .real for z in board )
35+ bottom = [z for z in board if z .real == max_real ]
36+ 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 the
4+ ones 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 beams
7+ from higher up.
8+
9+ By noting that no two splitters are ever horizontally adjacent, we can
10+ simplify the iteration logic a good deal.
11+ """
12+
13+ with open ("input" ) as f :
14+ ls = f .read ().strip ().split ("\n " )
15+
16+ splitters = [col for l in ls for col , x in enumerate (l ) if x == "^" ]
17+ entering = [1 ] + [0 ] * (len (splitters ) - 1 )
18+
19+ for i , si in enumerate (splitters ):
20+ for j , sj in enumerate (splitters [:i ][::- 1 ]):
21+ if si == sj :
22+ break
23+ if abs (si - sj ) == 1 :
24+ entering [i ] += entering [i - j - 1 ]
25+
26+ # Part 1
27+ print (sum (b > 0 for b in entering ))
28+
29+ # Part 2
30+ print (sum (entering ) + 1 )
You can’t perform that action at this time.
0 commit comments