| @@ -0,0 +1,42 @@ | |||
| import re | |||
| from helpers import DEBUG, debug, 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) | |||
| @@ -0,0 +1,41 @@ | |||
| import re | |||
| from helpers import DEBUG, debug, 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) | |||
| games_power = 0 | |||
| for game in games: | |||
| games_power += (game["red"] * game["blue"] * game["green"]) | |||
| print(games_power) | |||
| if __name__ == "__main__": | |||
| main(red_max=12, blue_max=14, green_max=13) | |||
| @@ -0,0 +1,13 @@ | |||
| DEBUG = False | |||
| def debug(message, *args, **kwargs): | |||
| global DEBUG | |||
| if DEBUG: | |||
| print(message, *args, **kwargs) | |||
| def load_input(day): | |||
| day = str(day) if day >= 10 else f"0{day}" | |||
| filename = f"day{day}.input" | |||
| with open(filename, "r") as file: | |||
| input_list = [line.strip() for line in file.readlines()] | |||
| return input_list | |||