maxs = {"red": 12, "green": 13, "blue": 14} gid = 0 possible_gid_sum = 0 with open("input") as lines: for line in lines.readlines(): gid += 1 line = line.rstrip() game_full, sets = line.split(":") game, gid_str = game_full.split(" ") assert int(gid_str) == gid possible = True for seet in sets.split(";"): gcs = {"red": 0, "green": 0, "blue": 0} for color in seet.split(","): amount_str, color = color.strip().split(" ") amount = int(amount_str) gcs[color] += amount for color, amount in gcs.items(): max = maxs[color] if amount > max: possible = False if possible: possible_gid_sum += gid print(gid, possible) print(possible_gid_sum)