From 7ad046492a05b0f3da793d8ab90da021a0afcfc2 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sat, 2 Dec 2023 12:49:28 +0100 Subject: day two part two --- 02/main.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to '02') diff --git a/02/main.py b/02/main.py index 59f722b..4d5cdbf 100755 --- a/02/main.py +++ b/02/main.py @@ -2,13 +2,17 @@ import sys import re -id_sum = 0 +sum = 0 for line in sys.stdin: line = line.strip() game_id, sets = line.split(":") game_id = int(re.sub("[^0-9]", "", game_id)) sets = [x.strip() for x in sets.split(";")] - possible = True + min_cubes = { + "red": 0, + "green": 0, + "blue": 0, + } for set in sets: cubes = [x.strip() for x in set.split(",")] cubes = [{"color": x.split()[1], "count": int(x.split()[0])} for x in cubes] @@ -20,13 +24,11 @@ for line in sys.stdin: for cube in cubes: map[cube["color"]] = cube["count"] - if map["red"] > 12 or map["green"] > 13 or map["blue"] > 14: - possible = False - break - if not possible: - continue + for color in map: + min_cubes[color] = max(min_cubes[color], map[color]) + power = min_cubes["red"] * min_cubes["green"] * min_cubes["blue"] - id_sum += game_id + sum += power -print(f">> {id_sum}") +print(f">> {sum}") -- cgit v1.2.3