import re def parse_line(line): match = re.match(r"Game (\d+): (.+)", line) game_id = int(match.group(1)) cubes = parse_cubes(match.group(2)) return (game_id, cubes) def parse_cubes(cubes): return [parse_pull(pull) for pull in cubes.split("; ")] def parse_pull(pull): r = g = b = 0 for color in pull.split(", "): [n, c] = color.split(" ") if c == "red": r += int(n) elif c == "green": g += int(n) else: b += int(n) return (r, g, b) def process_input(infile): power_sum = 0 for game in [parse_line(line) for line in infile]: r = g = b = 0 for pr, pg, pb in game[1]: r = max(r, pr) g = max(g, pg) b = max(b, pb) power_sum += r * g * b return power_sum with open("day02/data/input100.txt") as infile: result = process_input(infile) print(result) print(result == 2286) with open("day02/data/input101.txt") as infile: result = process_input(infile) print(result) print(result == 67335)