import sys
from dataclasses import dataclass, field

DEBUG = True
debug_padding = 20

def debug_print(printname, *args):
    if DEBUG:
        print(f"{'DEBUG::'+printname:<{debug_padding}}", *args)

#==========================================================================================


def get_points(match_count):
    return 0 if match_count==0 else 2**(match_count-1)

@dataclass
class Card:
    card_no: int = 0
    match_count: int = 0
    copies: int = 1
    #technically unneeded ----
    #winning_numbers: set = field(default_factory=set)
    #my_numbers: list = field(default_factory=list)

cards = []
total_points = 0

print("Parsing Input:")
print("")
print("===================================\n")

FILENAME = "/home/icaema/Public/advent-of-code/day-04/data/p02data.txt"
f = open(FILENAME, "r")

#for line in sys.stdin:
for line in f.readlines():
    line = line[5:] #remove "Card "
    debug_print("input",  line.strip())
    [card_number_side, given_number_side] = line.split(':')

    card_number = int(card_number_side)
    [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())
    my_numbers = [int(mn) for mn in my_numbers.split()]
    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)

    current_card = Card(card_number, match_count) #1,winning_numbers, my_numbers)
    cards.append(current_card)
    #debug_print("parsed", current_card)


print("")
print(" Proccessing:")
print("===================================\n")


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:
    for card in cards:
        debug_print("final_cards_and_copies", card)

#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(" Answer:")
print("===================================\n")
print(f"Total Points: \n{total_points}")
print(f"Total Cards: \n{total_card_copies}")