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 9bbc25e
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions 2024/day15/solutions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
with open("input") as f:
ls, moves = f.read().strip().split("\n\n")

dirs = {"^": -1, "v": 1, "<": -1j, ">": 1j}
moves = [dirs[x] for x in moves.replace("\n", "")]


def solve(part2):
walls = set()
boxes = set()

for i, l in enumerate(ls.split("\n")):
for j, x in enumerate(l):
z = i + j * (2j if part2 else 1j)
match x:
case "#":
walls |= {z, z + 1j} if part2 else {z}
case "O":
boxes.add(z)
case "@":
robot = z

for dz in moves:
to_move = set()
to_check = [robot + dz]
while to_check:
z = to_check.pop()
is_right_side = part2 and z - 1j in boxes
if z in boxes or is_right_side:
to_move.add(z)
to_check.append(z + dz)
if part2 and dz.real:
other = z - 1j if is_right_side 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 9bbc25e

Please sign in to comment.