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))