StyleInCode

RSS

 

Day 11 - Advent of Code 2024

11 December 2024

Working solutions for the day 11 puzzles.

Part One

""" day_11_01.py """

# usage: python3 day_11_01.py 25 < input

import sys


def process(items):
    """ process list per puzzle description """
    out = []
    for i in items:
        if i == 0:
            out.extend([1])
        elif len(text := str(i)) % 2 == 0:
            j = len(text) // 2
            out.extend([int(text[:j]), int(text[j:])])
        else:
            out.extend([i * 2024])
    return out


blinks = int(sys.argv[1])

with sys.stdin as infile:
    stones = list(map(int, infile.read().split()))

while blinks > 0:
    stones = process(stones)
    blinks -= 1

print(len(stones))

Part Two

""" day_11_02.py """

# usage: python3 day_11_02.py 75 < input

import sys


def process(items):
    """ process dict per puzzle description """
    out = {}
    for i, qty in items.items():
        if i == 0:
            out[1] = out.setdefault(1, 0) + qty
        elif len(text := str(i)) % 2 == 0:
            j = len(text) // 2
            lhs, rhs = int(text[:j]), int(text[j:])
            out[lhs] = out.setdefault(lhs, 0) + qty
            out[rhs] = out.setdefault(rhs, 0) + qty
        else:
            out[i * 2024] = out.setdefault(i * 2024, 0) + qty
    return out


blinks = int(sys.argv[1])

with sys.stdin as infile:
    raw = list(map(int, infile.read().split()))

stones = dict(zip(raw, len(raw) * [1]))

while blinks > 0:
    stones = process(stones)
    blinks -= 1

print(sum(stones.values()))

Categories

Links