import re from helpers import Helper helper = Helper(debug=True) debug = helper.debug load_input = helper.load_input def split_line(line, character): return [group.strip() for group in line.split(character)] def main(red_max, blue_max, green_max): lines = load_input(day=2) games = [] for line in lines: first_split = split_line(line, ":") game_num = int(re.match(r"Game (\d+)", first_split[0])[1]) line_dict = { "game": game_num, "red": 0, "green": 0, "blue": 0 } second_split = split_line(first_split[1], ";") for group in second_split: third_split = split_line(group, ",") for subgroup in third_split: matches = re.match(r"(\d+) ([a-z]+)", subgroup) color = matches[2] number = int(matches[1]) if number > line_dict[color]: line_dict[color] = number games.append(line_dict) matching_games = 0 for game in games: if game["red"] <= red_max and game["green"] <= green_max and game["blue"] <= blue_max: matching_games += game["game"] print(matching_games) if __name__ == "__main__": main(red_max=12, blue_max=14, green_max=13)