File tree 1 file changed +49
-0
lines changed
1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ with open ("input" ) as f :
2
+ ls , moves = f .read ().strip ().split ("\n \n " )
3
+
4
+ dirs = {"^" : - 1 , "v" : 1 , "<" : - 1j , ">" : 1j }
5
+ moves = [dirs [x ] for x in moves .replace ("\n " , "" )]
6
+
7
+
8
+ def solve (part2 ):
9
+ walls = set ()
10
+ boxes = set ()
11
+
12
+ for i , l in enumerate (ls .split ("\n " )):
13
+ for j , x in enumerate (l ):
14
+ z = i + j * (2j if part2 else 1j )
15
+ match x :
16
+ case "#" :
17
+ walls |= {z , z + 1j } if part2 else {z }
18
+ case "O" :
19
+ boxes .add (z )
20
+ case "@" :
21
+ robot = z
22
+
23
+ for dz in moves :
24
+ to_move = set ()
25
+ to_check = [robot + dz ]
26
+ while to_check :
27
+ z = to_check .pop ()
28
+ is_right_side = part2 and (left := z - 1j ) in boxes
29
+ if z in boxes or is_right_side :
30
+ to_move .add (left if is_right_side else z )
31
+ to_check .append (z + dz )
32
+ if part2 and dz .real :
33
+ other = left if is_right_side else z + 1j
34
+ to_check .append (other + dz )
35
+ elif z in walls :
36
+ break
37
+ else :
38
+ boxes -= to_move
39
+ boxes |= {w + dz for w in to_move }
40
+ robot += dz
41
+ tot = sum (boxes )
42
+ return tot .real * 100 + tot .imag
43
+
44
+
45
+ # Part 1
46
+ print (solve (False ))
47
+
48
+ # Part 2
49
+ print (solve (True ))
You can’t perform that action at this time.
0 commit comments