Cleaned up answer addeded some asserts
This commit is contained in:
parent
fdc1fea8dd
commit
ec546d9e19
|
|
@ -2,12 +2,15 @@ import sys
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
|
|
||||||
DEBUG = True
|
DEBUG = True
|
||||||
debug_padding = 24
|
debug_padding = 20
|
||||||
|
|
||||||
def debug_print(printname, *args):
|
def debug_print(printname, *args):
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
print(f"{'DEBUG::'+printname:<{debug_padding}}", *args)
|
print(f"{'DEBUG::'+printname:<{debug_padding}}", *args)
|
||||||
|
|
||||||
|
#==========================================================================================
|
||||||
|
|
||||||
|
|
||||||
def get_points(match_count):
|
def get_points(match_count):
|
||||||
return 0 if match_count==0 else 2**(match_count-1)
|
return 0 if match_count==0 else 2**(match_count-1)
|
||||||
|
|
||||||
|
|
@ -15,86 +18,69 @@ def get_points(match_count):
|
||||||
class Card:
|
class Card:
|
||||||
card_no: int = 0
|
card_no: int = 0
|
||||||
match_count: int = 0
|
match_count: int = 0
|
||||||
|
copies: int = 1
|
||||||
#technically unneeded ----
|
#technically unneeded ----
|
||||||
winning_numbers: set = field(default_factory=set)
|
#winning_numbers: set = field(default_factory=set)
|
||||||
my_numbers: list = field(default_factory=list)
|
#my_numbers: list = field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
cards = []
|
cards = []
|
||||||
total_points = 0
|
total_points = 0
|
||||||
|
|
||||||
#print("Parsing Input:")
|
print("Parsing Input:")
|
||||||
#print("")
|
print("")
|
||||||
#print("===================================\n");
|
print("===================================\n")
|
||||||
|
|
||||||
FILENAME = "/home/icaema/Public/advent-of-code/day-04/data/p02data.txt"
|
FILENAME = "/home/icaema/Public/advent-of-code/day-04/data/p02data.txt"
|
||||||
f = open(FILENAME, "r")
|
f = open(FILENAME, "r")
|
||||||
|
|
||||||
#for line in sys.stdin:
|
#for line in sys.stdin:
|
||||||
for line in f.readlines():
|
for line in f.readlines():
|
||||||
line = line[5:] #remove the "Card "
|
line = line[5:] #remove "Card "
|
||||||
#debug_print("input", line)
|
debug_print("input", line.strip())
|
||||||
[card_number_side, given_number_side] = line.split(':')
|
[card_number_side, given_number_side] = line.split(':')
|
||||||
|
|
||||||
card_number = int(card_number_side)
|
card_number = int(card_number_side)
|
||||||
#debug_print("aaaa", f"cns: -{card_number}-; gns: -{given_number_side}-")
|
|
||||||
[winning_numbers,my_numbers] = given_number_side.split('|')
|
[winning_numbers,my_numbers] = given_number_side.split('|')
|
||||||
|
#debug_print("parsing", f"cardNo: -{card_number}-; winNum: -{winning_numbers.strip()}-; myNum: -{my_numbers.strip()}-")
|
||||||
|
|
||||||
winning_numbers = set(int(wn) for wn in winning_numbers.split())
|
winning_numbers = set(int(wn) for wn in winning_numbers.split())
|
||||||
my_numbers = [int(mn) for mn in my_numbers.split()]
|
my_numbers = [int(mn) for mn in my_numbers.split()]
|
||||||
match_count = len([x for x in my_numbers if x in winning_numbers])
|
match_count = len([x for x in my_numbers if x in winning_numbers])
|
||||||
|
|
||||||
|
# cards wont have repeated my_numbers
|
||||||
|
if DEBUG: assert len(my_numbers) == len(set(my_numbers))
|
||||||
|
|
||||||
total_points += get_points(match_count)
|
total_points += get_points(match_count)
|
||||||
|
|
||||||
current_card = Card(card_number, match_count, winning_numbers, my_numbers)
|
current_card = Card(card_number, match_count) #1,winning_numbers, my_numbers)
|
||||||
cards.append(current_card)
|
cards.append(current_card)
|
||||||
#debug_print("parsed", current_card)
|
#debug_print("parsed", current_card)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
print("")
|
print("")
|
||||||
print(" Proccessing:")
|
print(" Proccessing:")
|
||||||
print("===================================\n")
|
print("===================================\n")
|
||||||
|
|
||||||
cards_and_copies = [{'card_no':card.card_no, 'match_count':card.match_count, 'copies':1} for card in cards]
|
|
||||||
|
for i in range(len(cards)):
|
||||||
|
card_processing = cards[i]
|
||||||
|
# i+1 is the cards number
|
||||||
|
if DEBUG: assert card_processing.card_no == i+1
|
||||||
|
#debug_print("loop_card", i, card_processing)
|
||||||
|
for j in range(card_processing.match_count):
|
||||||
|
cards[(i+1)+j].copies += card_processing.copies
|
||||||
|
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
#card number can also be calculated by its index+1
|
for card in cards:
|
||||||
for i, card in enumerate(cards_and_copies):
|
debug_print("final_cards_and_copies", card)
|
||||||
assert card['card_no'] == i+1
|
|
||||||
|
|
||||||
#debug_print("cards_and_copies", cards_and_copies)
|
|
||||||
|
|
||||||
for i in range(len(cards_and_copies)):
|
|
||||||
card_processing = cards_and_copies[i]
|
|
||||||
debug_print("loop_card", i, card_processing)
|
|
||||||
for j in range(card_processing['match_count']):
|
|
||||||
cards_and_copies[(i+1)+j]['copies'] += card_processing['copies']
|
|
||||||
|
|
||||||
for card in cards_and_copies:
|
|
||||||
debug_print("final print", card)
|
|
||||||
|
|
||||||
print("frick", sum([i['copies'] for i in cards_and_copies]))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#print("")
|
|
||||||
#print(" Calculate Answer:")
|
|
||||||
#print("===================================\n");
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#actually we do this earlier for the extra efficiency
|
||||||
|
#total_points = sum([get_points(card.match_count) for card in cards])
|
||||||
|
total_card_copies = sum([card.copies for card in cards])
|
||||||
|
|
||||||
|
|
||||||
print("")
|
print("")
|
||||||
print(" Answer:")
|
print(" Answer:")
|
||||||
print("===================================\n");
|
print("===================================\n")
|
||||||
print(f"Answer: \n{total_points}");
|
print(f"Total Points: \n{total_points}")
|
||||||
|
print(f"Total Cards: \n{total_card_copies}")
|
||||||
Loading…
Reference in New Issue