Sean McLemon | Advent of Code

Home | Czech | Blog | GitHub | Advent Of Code | Notes


2024-12-11 - Plutonian Pebbles

(original .ipynb)
from collections import defaultdict

puzzle_input_str = "0 4 4979 24 4356119 914 85734 698829"
test_input_str = """125 17"""


def parse_input(input_str):
    return input_str.split(" ")


def apply_rules(stone):
    if stone == "0":
        return ["1"]
        
    elif len(stone) % 2 == 0:
        pivot = len(stone) // 2
        return [
            str(int("".join(stone[:pivot]))),
            str(int("".join(stone[pivot:])))
        ]
    
    else:
        number = int("".join(stone)) * 2024
        return [str(number)]

    
def solve(input_str, iterations):
    stones = parse_input(input_str)
    stone_counts = defaultdict(lambda:0)

    for stone in stones:
        stone_counts[stone] += 1
    
    while iterations > 0:
        next_stone_counts = defaultdict(lambda:0)
        for stone, count in stone_counts.items():
            for next_stone in apply_rules(stone):
                next_stone_counts[next_stone] += count
        stone_counts = next_stone_counts
        iterations -= 1

    return sum(value for key, value in stone_counts.items())


def part_one(input_str):
    return solve(input_str, 25)


assert 55312 == part_one(test_input_str)

print("part one:", part_one(puzzle_input_str))
part one: 188902
def part_two(input_str):
    return solve(input_str, 75)


print("part two:", part_two(puzzle_input_str))
part two: 223894720281135