File tree 1 file changed +50
-0
lines changed
1 file changed +50
-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 z - 1j in boxes
29
+ if z in boxes or is_right_side :
30
+ to_move .add (z )
31
+ to_check .append (z + dz )
32
+ if part2 and dz .real :
33
+ other = z - 1j if is_right_side else z + 1j
34
+ to_move .add (other )
35
+ to_check .append (other + dz )
36
+ elif z in walls :
37
+ break
38
+ else :
39
+ to_move = to_move & boxes
40
+ boxes -= to_move
41
+ boxes |= {w + dz for w in to_move }
42
+ robot += dz
43
+ return sum (z .real * 100 + z .imag for z in boxes )
44
+
45
+
46
+ # Part 1
47
+ print (solve (False ))
48
+
49
+ # Part 2
50
+ print (solve (True ))
You can’t perform that action at this time.
0 commit comments