@@ -41,17 +41,45 @@ def solve(map_lines, move_sequence):
41
41
continue
42
42
43
43
if new_robot in boxes :
44
+ # Gather all cells for wide boxes:
44
45
box_positions = []
45
- current_pos = new_robot
46
- while current_pos in boxes :
47
- box_positions .append (current_pos )
48
- current_pos = (current_pos [0 ] + dx , current_pos [1 ] + dy )
49
-
50
- if grid [current_pos [1 ]][current_pos [0 ]] != '#' and current_pos not in boxes :
51
- for pos in reversed (box_positions ):
46
+ pending = [new_robot ]
47
+ visited = set ()
48
+ while pending :
49
+ pos = pending .pop ()
50
+ if pos not in visited and pos in boxes :
51
+ visited .add (pos )
52
+ box_positions .append (pos )
53
+ x2 , y2 = pos [0 ], pos [1 ]
54
+ # Check adjacent bracket if wide box
55
+ # Horizontal pair
56
+ if (x2 + 1 , y2 ) in boxes :
57
+ pending .append ((x2 + 1 , y2 ))
58
+ if (x2 - 1 , y2 ) in boxes :
59
+ pending .append ((x2 - 1 , y2 ))
60
+
61
+ # Vertical pair (rare but possible if squares placed vertically)
62
+ if (x2 , y2 + 1 ) in boxes :
63
+ pending .append ((x2 , y2 + 1 ))
64
+ if (x2 , y2 - 1 ) in boxes :
65
+ pending .append ((x2 , y2 - 1 ))
66
+
67
+ # Now push them in a chain
68
+ # ...existing code to detect free space...
69
+ new_pos_list = []
70
+ blocked = False
71
+ for pos in box_positions :
72
+ test_next = (pos [0 ] + dx , pos [1 ] + dy )
73
+ if grid [test_next [1 ]][test_next [0 ]] == '#' or test_next in boxes - set (box_positions ):
74
+ blocked = True
75
+ break
76
+ new_pos_list .append (test_next )
77
+
78
+ if not blocked :
79
+ for pos in box_positions :
52
80
boxes .remove (pos )
53
- new_pos = ( pos [ 0 ] + dx , pos [ 1 ] + dy )
54
- boxes .add (new_pos )
81
+ for pos in new_pos_list :
82
+ boxes .add (pos )
55
83
robot_pos = new_robot
56
84
else :
57
85
robot_pos = new_robot
@@ -89,7 +117,7 @@ def main():
89
117
90
118
# Part 2
91
119
result_part2 = solve_part2 (map_lines , move_sequence )
92
- print ("Part 2:" , result_part2 )
120
+ print ("Part 2:" , result_part2 ) # < 2697915
93
121
94
122
if __name__ == "__main__" :
95
123
main ()
0 commit comments