""" day_10_02.py """

# usage: python3 day_10_02.py

from itertools import product


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 push(control, status):
    """ push control with given status """
    return [value + 1 if i in control else value
            for i, value in enumerate(status)]


def parse_joltage(text):
    """ text to list """

    return list(int(i) for i in text[1:-1].split(','))


def parse_buttons(text):
    """ text to tuple """
    def t2t(text):
        """ text to tuple """
        return tuple(int(i) for i in text[1:-1].split(','))

    return [t2t(i) for i in text]


def solve(data):
    """ solve the puzzle """
    machines = [(parse_joltage(j), parse_buttons(b))
                for _, *b, j in [row.split() for row in data.splitlines()]]

    tally = []
    for goal, buttons in machines:
        least = float('inf')
        for combo in product(range(5), repeat=len(buttons)):
            state = [0 for _ in goal]
            for i, press in enumerate(combo):
                for _ in range(press):
                    state = push(buttons[i], state)
            if state == goal:
                least = min(least, sum(combo))
        tally.append(least)
        print(tally)  # debug

    return sum(tally)


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

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

    print(solution)
