StyleInCode

RSS

 

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


def move(command, across, plot):
    """ execute move command """
    delta = {'^': -across, '>': 1, 'v': across, '<': -1}
    area = list(plot)
    bot = area.index('@')

    i = 1
    while area[(j := bot + i * delta[command])] == 'O':
        i += 1

    if area[j] != '#':
        area[bot] = '.'
        area[bot + delta[command]] = '@'
        if i != 1:
            area[j] = 'O'

    return ''.join(area)


grid = ''
width = None
with sys.stdin as infile:
    while (line := infile.readline()) != '\n':
        for w, obj in enumerate(line.strip()):
            grid += obj
            width = w + 1
    orders = infile.read().replace('\n', '')

for instruction in orders:
    grid = move(instruction, width, grid)

print(sum(100 * (i // width) + i % width
          for i, box in enumerate(grid) if box == 'O'))

Part Two


Categories

Links