| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 | import re
from helpers import Helper
helper = Helper(debug=True)
debug = helper.debug
load_input = helper.load_input
def split_line(line, character):
    return [group.strip() for group in line.split(character)]
def main(red_max, blue_max, green_max):
    lines = load_input(day=2)
    games = []
    for line in lines:
        first_split = split_line(line, ":")
        game_num = int(re.match(r"Game (\d+)", first_split[0])[1])
        line_dict = {
            "game": game_num,
            "red": 0,
            "green": 0,
            "blue": 0
        }
        second_split = split_line(first_split[1], ";")
        for group in second_split:
            third_split = split_line(group, ",")
            for subgroup in third_split:
                matches = re.match(r"(\d+) ([a-z]+)", subgroup)
                color = matches[2]
                number = int(matches[1])
                if number > line_dict[color]:
                    line_dict[color] = number
        games.append(line_dict)
    games_power = 0
    for game in games:
        games_power += (game["red"] * game["blue"] * game["green"])
    print(games_power)
if __name__ == "__main__":
    main(red_max=12, blue_max=14, green_max=13)
 |