diff --git a/day-02/README.md b/day-02/README.md index d500e00..b981923 100644 --- a/day-02/README.md +++ b/day-02/README.md @@ -66,6 +66,8 @@ For each game, find the minimum set of cubes that must have been present.
What is the sum of the power of these sets?

-Your puzzle answer was XXXXX. +Your puzzle answer was 66909. + +Both parts of this puzzle are complete! They provide two gold stars: **

diff --git a/day-02/src/part02.py b/day-02/src/part02.py new file mode 100644 index 0000000..27b7b04 --- /dev/null +++ b/day-02/src/part02.py @@ -0,0 +1,97 @@ +import sys +from dataclasses import dataclass, field + +DEBUG = True +padding=24 + +def debug_print(printname, *args): + if not DEBUG: + return + print(f"{'DEBUG::'+printname:<{padding}}", *args) + pass + + +FILENAME = "/home/icaema/Public/advent-of-code/day-02/data/p02data.txt" + +bag_limits = { + 'red': 12, + 'green': 13, + 'blue': 14 +} + +empty_hand = { + 'red': 0, + 'green': 0, + 'blue': 0 +} + +@dataclass +class Game(): + id: int + hands: list + min_cubes: dict = field(default_factory=empty_hand.copy) + valid: bool = True + def validateGame(self): + for hand in self.hands: + for color in hand.keys(): + if hand[color] > bag_limits[color]: + debug_print("BAD HAND", hand) + self.valid = False + pass + def genMinCubes(self): + for hand in self.hands: + for color in hand.keys(): + if hand[color] > self.min_cubes[color]: + self.min_cubes[color] = hand[color] + pass + + +game_list = [] + +f = open(FILENAME, "r") + +#for line in sys.stdin: +for line in f.readlines(): + line = line.strip() + + debug_print("input", line) + two_split = line.split(":") + + game_id = int(two_split[0][5:]) + active_game = Game(game_id, []) + + raw_hands = two_split[1].split(";") + #debug_print("raw_hands", raw_hands) + for hand in raw_hands: + hand = hand.strip("").split(", ") + hand_dict = empty_hand.copy() + for hand_element in hand: + hand_element = hand_element.strip().split(" ") + hand_dict[hand_element[1]] = int(hand_element[0]) + #print(hand_dict) + active_game.hands.append(hand_dict) + debug_print("processed_game", active_game) + game_list.append(active_game) + + + +# processing the games + +for game in game_list: + debug_print("Processing Game", game.id) + game.validateGame() + game.genMinCubes() + + +[debug_print("post_processed", game) for game in game_list] +min_cubes_per_game = [game.min_cubes['red']*game.min_cubes['green']*game.min_cubes['blue'] for game in game_list] + + +#valid_games = [game.id for game in game_list if game.valid == True] + +print("") +print("Minimum Cubes per game:") +print(min_cubes_per_game) +print("===================================\n"); +print("Answer:"); +print(sum(min_cubes_per_game)) \ No newline at end of file