This year I try to record my attempt at solving the Advent of Code 2023 riddles. This is Day 4 - see https:adventofcode.com/2023/day/4
Part 1 #
Lets first read the task:
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.
For example:
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
In the above example, card 1 has five winning numbers (
41
,48
,83
,86
, and17
) and eight numbers you have (83
,86
,6
,31
,17
,9
,48
, and53
). Of the numbers you have, four of them (48
,83
,17
, and86
) are winning numbers! That means card 1 is worth8
points (1 for the first match, then doubled three times for each of the three matches after the first).
- Card 2 has two winning numbers (
32
and61
), so it is worth2
points.- Card 3 has two winning numbers (
1
and21
), so it is worth2
points.- Card 4 has one winning number (
84
), so it is worth1
point.- Card 5 has no winning numbers, so it is worth no points.
- Card 6 has no winning numbers, so it is worth no points.
So, in this example, the Elf’s pile of scratchcards is worth
13
points.Take a seat in the large pile of colorful cards. How many points are they worth in total?
My input file: 2023-12-04-1-aoc.txt
Loading this data is very similar to Day 2 - so let’s load the data as we did
there. Our goal is to get win
and yours
columns holding the respective
digits which we want to compare. We find the numbers with one or more digits
using the regex \d+
. And we want them to be in sets (not lists), as we can
logically compare sets in Python.
import pandas as pd
import re
txt = pd.read_table('data/2023-12-04-1-aoc.txt', names=['win'])
txt['id'] = txt.loc[:, 'win'].str.split(':').apply(
lambda x: int(x[0].strip('Card ')))
txt['win'] = (txt.loc[:, 'win']
.str.split(':').apply(lambda x: x[1]))
txt['yours'] = (txt.loc[:, 'win']
.str.split('|')
.apply(lambda x: x[1])
# get a list of only the numbers / digits
.apply(lambda x: re.findall(r'\d+', x))
# convert the list of strings to a set of integers
.apply(lambda x: set([int(i) for i in x])))
txt['win'] = (txt.loc[:, 'win']
.str.split('|')
.apply(lambda x: x[0])
.apply(lambda x: re.findall(r'\d+', x))
.apply(lambda x: set([int(i) for i in x])))
txt
win | id | yours | |
---|---|---|---|
0 | {32, 36, 7, 9, 10, 12, 82, 85, 95, 31} | 1 | {2, 7, 9, 10, 12, 14, 21, 22, 23, 24, 31, 32, … |
1 | {35, 76, 16, 82, 19, 22, 88, 59, 60, 95} | 2 | {7, 8, 12, 16, 19, 22, 26, 28, 35, 38, 44, 51,… |
2 | {1, 70, 11, 78, 48, 19, 52, 88, 28, 94} | 3 | {3, 4, 8, 17, 18, 19, 24, 31, 34, 45, 52, 54, … |
3 | {65, 2, 72, 28, 14, 16, 55, 91, 92, 62} | 4 | {3, 4, 6, 7, 8, 9, 15, 30, 33, 35, 47, 49, 51,… |
4 | {38, 41, 75, 77, 50, 24, 94, 60, 61, 30} | 5 | {1, 2, 4, 5, 6, 7, 9, 10, 14, 17, 21, 29, 47, … |
… | … | … | … |
213 | {97, 98, 39, 41, 43, 12, 13, 19, 93, 95} | 214 | {5, 10, 17, 20, 28, 29, 33, 34, 36, 50, 51, 52… |
214 | {97, 35, 69, 40, 74, 45, 20, 21, 62, 31} | 215 | {1, 8, 15, 17, 18, 25, 30, 33, 42, 44, 47, 52,… |
215 | {33, 70, 71, 12, 78, 17, 51, 86, 60, 94} | 216 | {7, 8, 9, 10, 22, 29, 37, 39, 41, 43, 46, 47, … |
216 | {98, 67, 68, 38, 70, 39, 72, 77, 45, 21} | 217 | {8, 21, 22, 25, 26, 31, 37, 41, 42, 48, 54, 57… |
217 | {34, 9, 44, 78, 79, 16, 17, 19, 55, 92} | 218 | {1, 4, 20, 21, 27, 38, 39, 40, 41, 45, 46, 52,… |
218 rows × 3 columns
Now, we get the
logical conjunction of win
and yours
, these are our winning
numbers. Then, the number of wins is converted to points - for all number of
wins bigger than 1, we can get the points by 2**(n_wins-1)
.
txt['n_wins'] = txt.apply(
lambda row: len(row.loc['win'] & row.loc['yours']), axis=1)
txt['points'] = txt.loc[:, 'n_wins'].apply(
lambda x: 2**(x-1) if x > 1 else x)
txt.loc[:, ['win', 'n_wins', 'points']]
win | n_wins | points | |
---|---|---|---|
0 | {32, 36, 7, 9, 10, 12, 82, 85, 95, 31} | 10 | 512 |
1 | {35, 76, 16, 82, 19, 22, 88, 59, 60, 95} | 10 | 512 |
2 | {1, 70, 11, 78, 48, 19, 52, 88, 28, 94} | 5 | 16 |
3 | {65, 2, 72, 28, 14, 16, 55, 91, 92, 62} | 0 | 0 |
4 | {38, 41, 75, 77, 50, 24, 94, 60, 61, 30} | 0 | 0 |
… | … | … | … |
213 | {97, 98, 39, 41, 43, 12, 13, 19, 93, 95} | 0 | 0 |
214 | {97, 35, 69, 40, 74, 45, 20, 21, 62, 31} | 0 | 0 |
215 | {33, 70, 71, 12, 78, 17, 51, 86, 60, 94} | 2 | 2 |
216 | {98, 67, 68, 38, 70, 39, 72, 77, 45, 21} | 1 | 1 |
217 | {34, 9, 44, 78, 79, 16, 17, 19, 55, 92} | 0 | 0 |
218 rows × 3 columns
Then, we just sum up:
sum(txt.loc[:, 'points'])
25004
Part 2 #
Let’s read the task of 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.)
This time, the above example goes differently:
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
- Card 1 has four matching numbers, so you win one copy each of the next four cards: cards 2, 3, 4, and 5.
- Your original card 2 has two matching numbers, so you win one copy each of cards 3 and 4.
- Your copy of card 2 also wins one copy each of cards 3 and 4.
- Your four instances of card 3 (one original and three copies) have two matching numbers, so you win four copies each of cards 4 and 5.
- Your eight instances of card 4 (one original and seven copies) have one matching number, so you win eight copies of card 5.
- Your fourteen instances of card 5 (one original and thirteen copies) have no matching numbers and win no more cards.
Once all of the originals and copies have been processed, you end up with
1
instance of card 1,2
instances of card 2,4
instances of card 3,8
instances of card 4,14
instances of card 5, and1
instance of card 6. In total, this example pile of scratchcards causes you to ultimately have30
scratchcards!Process all of the original and copied scratchcards until no more scratchcards are won. Including the original set of scratchcards, how many total scratchcards do you end up with?
We will use the n_wins
column we created before and go from there. We step
through each Game. Each current game gets +1 card. Then, we step through the
number of next games depending on our n_wins
. Each of these gets added the
card number of the current game.
txt['cards'] = 0
for i, nwin in enumerate(txt.loc[:, 'n_wins']):
txt.loc[i, 'cards'] += 1
for j in range(1, nwin+1):
txt.loc[i+j, 'cards'] += txt.loc[i, 'cards']
txt.loc[:, ['n_wins', 'cards']]
n_wins | cards | |
---|---|---|
0 | 10 | 1 |
1 | 10 | 2 |
2 | 5 | 4 |
3 | 0 | 8 |
4 | 0 | 8 |
… | … | … |
213 | 0 | 9608 |
214 | 0 | 8927 |
215 | 2 | 8927 |
216 | 1 | 12636 |
217 | 0 | 21564 |
218 rows × 2 columns
Now, we just sum the output again:
sum(txt['cards'])
14427616