Skip to content

Commit

Permalink
Add solution to 2024-12-15
Browse files Browse the repository at this point in the history
  • Loading branch information
fuglede committed Dec 15, 2024
1 parent 7ab4578 commit 02d6404
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions 2024/day15/solutions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
with open("input") as f:
ls, moves = f.read().strip().split("\n\n")

dirs = {"^": -1, "v": 1, "<": -1j, ">": 1j}
moves = list(map(dirs.__getitem__, moves.replace("\n", "")))
board = {i + 1j * j: x for i, l in enumerate(ls.split("\n")) for j, x in enumerate(l)}


def solve(part2):
walls = {z for z, x in board.items() if x == "#"}
boxes = {z for z, x in board.items() if x == "O"}
robot = next(z for z, x in board.items() if x == "@")
if part2:
walls = {w.real + 2 * w.imag * 1j for w in walls}
walls |= {z + 1j for z in walls}
boxes = {b.real + 2 * b.imag * 1j for b in boxes}
robot = robot.real + 2 * robot.imag * 1j
for dz in moves:
to_move = set()
to_check = [robot + dz]
while to_check:
z = to_check.pop()
if z in boxes or (part2 and z - 1j in boxes):
to_move.add(z)
to_check.append(z + dz)
if part2 and dz.real:
other = z + 1j if z in boxes else z - 1j
to_move.add(other)
to_check.append(other + dz)
elif z in walls:
break
else:
to_move = to_move & boxes
boxes -= to_move
boxes |= {w + dz for w in to_move}
robot += dz
return sum(z.real * 100 + z.imag for z in boxes)


# Part 1
print(solve(False))

# Part 2
print(solve(True))

0 comments on commit 02d6404

Please sign in to comment.