90 lines
1.8 KiB
Python
90 lines
1.8 KiB
Python
import sys
|
|
from dataclasses import dataclass
|
|
|
|
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/p01data.txt"
|
|
|
|
bag_limits = {
|
|
'red': 12,
|
|
'green': 13,
|
|
'blue': 14
|
|
}
|
|
|
|
empty_hand = {
|
|
'red': 0,
|
|
'green': 0,
|
|
'blue': 0
|
|
}
|
|
|
|
@dataclass
|
|
class Game():
|
|
id: int
|
|
hands: list
|
|
valid: bool = True
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
# MUTATES THE GAME OBJ
|
|
def validateGame(game):
|
|
for hand in game.hands:
|
|
for color in hand.keys():
|
|
if hand[color] > bag_limits[color]:
|
|
debug_print("BAD HAND", hand)
|
|
game.valid = False
|
|
pass
|
|
|
|
|
|
|
|
|
|
# processing the games
|
|
|
|
for game in game_list:
|
|
debug_print("Processing Game", game.id)
|
|
validateGame(game)
|
|
|
|
|
|
|
|
valid_games = [game.id for game in game_list if game.valid == True]
|
|
|
|
print("")
|
|
print("Valid Games:")
|
|
print(valid_games)
|
|
print("===================================\n");
|
|
print("Answer: ", sum(valid_games)); |