cleaned up code and added debug print

This commit is contained in:
icaema 2023-12-02 11:42:01 -05:00
parent 72a7c14ea5
commit 7a3d588b84
1 changed files with 20 additions and 12 deletions

View File

@ -1,7 +1,15 @@
import sys
import logging
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"
@ -23,6 +31,7 @@ class Game():
hands: list
valid: bool = True
game_list = []
f = open(FILENAME, "r")
@ -30,13 +39,15 @@ 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, [])
#print("DEBUG: ", game_list)
raw_hands = two_split[1].split(";")
#print("DEBUG:", raw_hands)
#debug_print("raw_hands", raw_hands)
for hand in raw_hands:
hand = hand.strip("").split(", ")
hand_dict = empty_hand.copy()
@ -45,7 +56,7 @@ for line in f.readlines():
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)
@ -55,7 +66,7 @@ def validateGame(game):
for hand in game.hands:
for color in hand.keys():
if hand[color] > bag_limits[color]:
print("BAD HAND", hand)
debug_print("BAD HAND", hand)
game.valid = False
pass
@ -65,9 +76,11 @@ def validateGame(game):
# processing the games
for game in game_list:
print("Processing Game:", game)
debug_print("Processing Game", game.id)
validateGame(game)
valid_games = [game.id for game in game_list if game.valid == True]
print("")
@ -75,8 +88,3 @@ print("Valid Games:")
print(valid_games)
print("===================================\n");
print("Answer: ", sum(valid_games));
#Game 1: 4 blue, 4 red, 16 green; 14 green, 5 red; 1 blue, 3 red, 5 green