Day 15 - Advent of Code 2024
15 December 2024
Working solutions for the day 15 puzzles.
Part One
""" day_15_01.py """
# usage: python3 day_15_01.py < input
import sys
from collections import namedtuple
Point = namedtuple('GridPoint', 'x, y')
def find_bot(plan):
""" where's Robbie """
return [point for point, item in plan.items() if item == '@'][0]
def execute(commands, plan):
""" command Robbie """
deltas = {'^': (0, -1), '>': (1, 0), 'v': (0, 1), '<': (-1, 0)}
cplan = plan.copy()
i = find_bot(cplan)
for move in commands:
dx, dy = deltas[move]
j = Point(i.x + dx, i.y + dy)
match cplan[j]:
case '.':
cplan[i] = '.'
cplan[j] = '@'
i = j
case 'O':
while cplan[j] not in ['.', '#']:
j = Point(j.x + dx, j.y + dy)
if cplan[j] == '.':
cplan[j] = 'O'
cplan[i] = '.'
j = Point(i.x + dx, i.y + dy)
cplan[j] = '@'
i = j
return cplan
def ingest():
""" parse standard input """
plan = {}
with sys.stdin as infile:
point = Point(0, 0)
for line in infile:
if line.strip() == '':
break
for item in line.strip():
plan[point] = item
point = Point(point.x + 1, point.y)
point = Point(0, point.y + 1)
return infile.read().replace('\n', ''), plan
moves, grid = ingest()
grid = execute(moves, grid)
print(sum(x + 100 * y for (x, y), item in grid.items() if item == 'O'))
Part Two