Skip to content

Commit 622c7ce

Browse files
o1: day 15 part 2 double width boxes - fails
For part 2, each box can directly push two other boxes at once. In #selection We're currently only considering things in the same column, but need to look at both columns for each box being pushed
1 parent 6a7fec6 commit 622c7ce

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

2024/day15.py

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,45 @@ def solve(map_lines, move_sequence):
4141
continue
4242

4343
if new_robot in boxes:
44+
# Gather all cells for wide boxes:
4445
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:
5280
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)
5583
robot_pos = new_robot
5684
else:
5785
robot_pos = new_robot
@@ -89,7 +117,7 @@ def main():
89117

90118
# Part 2
91119
result_part2 = solve_part2(map_lines, move_sequence)
92-
print("Part 2:", result_part2)
120+
print("Part 2:", result_part2) # < 2697915
93121

94122
if __name__ == "__main__":
95123
main()

0 commit comments

Comments
 (0)