advent-of-code/2024/15/one.py
2024-12-25 12:59:49 +01:00

76 lines
1.5 KiB
Python

#!/usr/bin/env python3
import sys
input_file = sys.argv[1]
with open(input_file) as fd:
lines = [line.rstrip() for line in fd.readlines()]
coords = tuple[int, int]
pos: coords
box = list()
moves: list[coords] = list()
directions = {
"^": (-1, 0), # ^
">": (0, 1), # >
"v": (1, 0), # v
"<": (0, -1), # <
}
boxing = True
for i, line in enumerate(lines):
if not line:
boxing = False
elif boxing:
bline = list(line)
if "@" in bline:
j = bline.index("@")
pos = i, j
# bline[j] = "."
box.append(bline)
else:
for c in line:
direction = directions[c]
moves.append(direction)
def print_box() -> None:
for bline in box:
print("".join(bline))
print()
print_box()
for move in moves:
first = pos[0] + move[0], pos[1] + move[1]
last = first
possible = True
while True:
c = box[last[0]][last[1]]
# print(c)
if c == ".":
break
elif c == "#":
possible = False
break
last = last[0] + move[0], last[1] + move[1]
if possible:
if first != last:
box[last[0]][last[1]] = "O"
box[pos[0]][pos[1]] = "."
box[first[0]][first[1]] = "@"
pos = first
print(move, possible, pos, first, last)
print_box()
score = 0
for i, bline in enumerate(box):
for j, char in enumerate(bline):
if char == "O":
score += 100 * i + j
print(score)