StyleInCode

RSS

 

Day 3 - Advent of Code 2025

3 December 2025

Working solutions for the day 3 puzzles.

Part One

""" day_03_01.py """

# usage: python3 day_03_01.py

from itertools import combinations


def get(filename):
    """ contents of filename """
    with open(filename, 'r', encoding='utf-8') as infile:
        data = infile.read().strip()

    return data


def test(data, given_solution):
    """ testing """
    assert solve(data) == given_solution


def max_joltage(battery_bank):
    """ largest battery number """
    return max(int(''.join(c)) for c in combinations(battery_bank, 2))


def solve(data):
    """ solve the puzzle """
    joltages = [max_joltage(bank) for bank in data.split()]

    return sum(joltages)


if __name__ == '__main__':
    test(get('example01'), 357)

    puzzle = get('input')
    solution = solve(puzzle)

    print(solution)

Part Two

""" day_03_02.py """

# usage: python3 day_03_02.py


def get(filename):
    """ contents of filename """
    with open(filename, 'r', encoding='utf-8') as infile:
        data = infile.read().strip()

    return data


def test(data, given_solution):
    """ testing """
    assert solve(data) == given_solution


def max_joltage(battery_bank):
    """ largest battery number """
    size = 12
    digits = battery_bank
    remove = len(digits) - size

    i = 0
    while remove > 0 and i < size:
        focus = digits[i:i + remove + 1]
        tail = digits[i + remove + 1:]

        if focus.find(max(focus)) == 0:
            i += 1
        else:
            digits = digits[:i] + focus[1:] + tail
            remove -= 1

    return int(digits[:size])


def solve(data):
    """ solve the puzzle """
    joltages = [max_joltage(bank) for bank in data.split()]

    return sum(joltages)


if __name__ == '__main__':
    test(get('example01'), 3121910778619)

    puzzle = get('input')
    solution = solve(puzzle)

    print(solution)

Categories

Links