#!/usr/bin/env python # -*- encoding: utf-8; py-indent-offset: 4 -*- # Author: Sebastian Mark # CC-BY-SA (https://creativecommons.org/licenses/by-sa/4.0/deed.de) # pylint: disable=missing-module-docstring,missing-function-docstring,consider-using-f-string import re def readinput(): with open("input", "r", encoding="utf-8") as file: lines = file.readlines() return lines def is_playable(line: str, max_cubes: tuple) -> bool: games = line.split(";") for game in games: cube_count = {"red": 0, "green": 0, "blue": 0} regex = r"(\d+) (\w+)" matches = re.findall(regex, game) for m in matches: color = m[1] count = int(m[0]) cube_count[color] += count for color, count in cube_count.items(): if count > max_cubes[color]: return False return True def get_game_nr(line: str) -> int: regex = r"Game (\d+):" matches = re.findall(regex, line) return int(matches[0]) def get_cube_power(line: str) -> int: cube_count = {"red": 0, "green": 0, "blue": 0} regex = r"(\d+) (\w+)" matches = re.findall(regex, line) for m in matches: color = m[1] count = int(m[0]) if count > cube_count[color]: cube_count[color] = count product = 1 for value in cube_count.values(): product *= value return product def main(): lines = readinput() max_cubes = { "red": 12, "green": 13, "blue": 14, } # part 1 count = 0 for line in lines: if is_playable(line, max_cubes): count += get_game_nr(line) print("Playable games: %d" % count) # part 2 count = 0 for line in lines: count += get_cube_power(line) print("Set power sum: %d" % count) if __name__ == "__main__": main()