Day 4 - Discoded/adventofcode GitHub Wiki

Day 4

The Elf leads you over to the pile of colorful cards. There, you discover dozens of scratchcards, all with their opaque covering already scratched off. Picking one up, it looks like each card has two lists of numbers separated by a vertical bar (|): a list of winning numbers and then a list of numbers you have. You organize the information into a table (your puzzle input).

As far as the Elf has been able to figure out, you have to figure out which of the numbers you have appear in the list of winning numbers. The first match makes the card worth one point and each match after the first doubles the point value of that card.

Input:

Card 1: 41 48 83 86 17 | 83 86  6 31 17  9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3:  1 21 53 59 44 | 69 82 63 72 16 21 14  1
Card 4: 41 92 73 84 69 | 59 84 76 51 58  5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11

Output:

Card 1: 41, 48, 83, 86 -> 2^(4-1) = 8
Card 2: 32, 61 -> 2^(2-1) = 2
Card 3: 1, 21 -> 2^(2-1) = 2
Card 4: 84 -> 2^0 = 1
Card 5: 0
Card 6: 0

Final answer: 13 (Sum total of points)

Solution:

Split each line into winning_numbers and owned_numbers
for x in winning_numbers:
            for y in owned_numbers:
                if(x == y):
                    points += 1
    sum_of_points += 2^(points - 1)

Day 4 Part 2

There's no such thing as "points". Instead, scratchcards only cause you to win more scratchcards equal to the number of winning numbers you have.

Specifically, you win copies of the scratchcards below the winning card equal to the number of matches. So, if card 10 were to have 5 matching numbers, you would win one copy each of cards 11, 12, 13, 14, and 15.

Copies of scratchcards are scored like normal scratchcards and have the same card number as the card they copied. So, if you win a copy of card 10 and it has 5 matching numbers, it would then win a copy of the same cards that the original card 10 won: cards 11, 12, 13, 14, and 15. This process repeats until none of the copies cause you to win any more cards. (Cards will never make you copy a card past the end of the table.)

Assuming you have:
    the list of points for each card: points_list
        where points_list[i] = total points on the ith card
    
Create a list where each card represent a copy of each card, the_duplicates = [0, 1, 1, ..., 1]
for i, elem in enumerate(the_duplicates):

        while the_duplicates[i] != 0:

            # Subtract 1 Card from list of duplicates
            the_duplicates[i] -= 1
            # Add 1 Card to total number of cards
            card_count += 1

            print("Card {}, points: {}".format(i, points_list[i]))
            points = points_list[i]

            for x in range(1, points+1):
               
                print("extra card: ", i+x)
                # Checks if the extra card is not past the end of the table
                # (Cards will never make you copy a card past the end of the table.)
                if not (i+x > len(points_list)-1):
                    the_duplicates[i+x] += 1