""" day_06_02.py """

# usage: python3 day_06_02.py

from math import prod


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

    return data


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


def parse(column):
    """ operation and number """
    match column[-1]:
        case '+':
            op = sum
        case '*':
            op = prod
        case _:
            op = None

    return op, int(column) if op is None else int(column[:-1])


def solve(data):
    """ solve the puzzle """
    worksheet = {}
    _ = [worksheet.setdefault(c, []).append(col) for row in data.splitlines()
         for c, col in enumerate(row)]

    problem = 0
    operations = {}
    for _, line in worksheet.items():
        text = ''.join(line).strip()
        if text == '':
            problem += 1
            continue

        func, number = parse(text)

        args = operations.setdefault(problem, (func, [number]))[1]
        if func is None:
            args.append(number)

    return sum(func(args) for _, (func, args) in operations.items())


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

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

    print(solution)
