from collections import defaultdict from tools.aoc import AOCDay from tools.math import mul from typing import Any class Day(AOCDay): inputs = [ [ (8, "input2_test"), (1734, "input2_dennis"), (2551, "input2"), ], [ (2286, "input2_test"), (70387, "input2_dennis"), (62811, "input2"), ], ] def part1(self) -> Any: MAX = { "red": 12, "green": 13, "blue": 14, } possible_sum = 0 for game in self.getInput(): game_name, draws = game.split(": ") _, game_id = game_name.split() impossible = False for draw in draws.split("; "): if impossible: break for color_draw in draw.split(", "): count, color = color_draw.split() if int(count) > MAX[color]: impossible = True if not impossible: possible_sum += int(game_id) return possible_sum def part2(self) -> Any: set_power_sum = 0 for game in self.getInput(): _, draws = game.split(": ") min_color = defaultdict(int) for draw in draws.split("; "): for color_draw in draw.split(", "): count, color = color_draw.split() min_color[color] = max(min_color[color], int(count)) set_power_sum += mul(min_color.values()) return set_power_sum if __name__ == "__main__": day = Day(2023, 2) day.run(verbose=True)