day 4 complete
This commit is contained in:
@@ -3,4 +3,36 @@ import os
|
|||||||
with open(r'advent_of_code\2023\day_4\input.txt', 'r') as file:
|
with open(r'advent_of_code\2023\day_4\input.txt', 'r') as file:
|
||||||
input = file.readlines()
|
input = file.readlines()
|
||||||
|
|
||||||
print(input)
|
# print(input)
|
||||||
|
test_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'''
|
||||||
|
|
||||||
|
#input = test_input.split('\n')
|
||||||
|
points = 0
|
||||||
|
for line in input:
|
||||||
|
print(line)
|
||||||
|
winning_numbers = []
|
||||||
|
my_numbers = []
|
||||||
|
line = line.split('|')
|
||||||
|
winning_numbers = line[0].split(':')[1].split()
|
||||||
|
my_numbers = line[1].split()
|
||||||
|
print(f'my numbers are: {my_numbers}')
|
||||||
|
print(f'winning numbers are: {winning_numbers}')
|
||||||
|
wins = 0
|
||||||
|
for number in my_numbers:
|
||||||
|
if number in winning_numbers:
|
||||||
|
wins += 1
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
if wins > 0:
|
||||||
|
add_to_points = 2**wins/2
|
||||||
|
print(f'add to points: {add_to_points}')
|
||||||
|
points += add_to_points
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
print(f'wins: {wins} & points: {points}')
|
||||||
|
print(f'points: {points}')
|
||||||
@@ -1,6 +1,55 @@
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
with open(r'advent_of_code\2023\day_4\input.txt', 'r') as file:
|
with open(r'advent_of_code\2023\day_4\input.txt', 'r') as file:
|
||||||
input = file.read()
|
input = file.readlines()
|
||||||
|
|
||||||
|
# print(input)
|
||||||
|
test_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'''
|
||||||
|
|
||||||
|
#input = test_input.split('\n')
|
||||||
|
|
||||||
|
card_count = {}
|
||||||
|
for card in range(1,len(input)+1):
|
||||||
|
card_count[card] = 1
|
||||||
|
|
||||||
|
for line in input:
|
||||||
|
winning_numbers = []
|
||||||
|
my_numbers = []
|
||||||
|
line = line.split('|')
|
||||||
|
card_number = line[0].split(':')[0].split()[1]
|
||||||
|
winning_numbers = line[0].split(':')[1].split()
|
||||||
|
my_numbers = line[1].split()
|
||||||
|
print(f'card number: {card_number}')
|
||||||
|
#print(f'my numbers are: {my_numbers}')
|
||||||
|
#print(f'winning numbers are: {winning_numbers}')
|
||||||
|
|
||||||
|
# loop for the amount of times we have a duplciate card
|
||||||
|
for i in range(card_count[int(card_number)]):
|
||||||
|
wins = 0
|
||||||
|
for number in my_numbers:
|
||||||
|
if number in winning_numbers:
|
||||||
|
wins += 1
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
if wins > 0:
|
||||||
|
#print(f'wins: {wins}')
|
||||||
|
# add 1 duplicate card for each card following this one
|
||||||
|
for win in range(int(wins)):
|
||||||
|
add_duplicates = int(card_number) + 1 + win
|
||||||
|
#print(f'add duplicates: {add_duplicates}')
|
||||||
|
if add_duplicates in card_count:
|
||||||
|
card_count[add_duplicates] += 1
|
||||||
|
else:
|
||||||
|
card_count[add_duplicates] += 0
|
||||||
|
#print(f'card_count: {card_count}')
|
||||||
|
|
||||||
|
print(f'card_count: {card_count}')
|
||||||
|
#sum up all the values in the dictionary
|
||||||
|
total_cards = sum(card_count.values())
|
||||||
|
print(f'total cards: {total_cards}')
|
||||||
|
|
||||||
print(input)
|
|
||||||
|
|||||||
@@ -33,4 +33,33 @@ Card 5 has no winning numbers, so it is worth no points.
|
|||||||
Card 6 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.
|
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?
|
Take a seat in the large pile of colorful cards. How many points are they worth in total?
|
||||||
|
|
||||||
|
--- Part Two ---
|
||||||
|
Just as you're about to report your findings to the Elf, one of you realizes that the rules have actually been printed on the back of every card this whole time.
|
||||||
|
|
||||||
|
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.
|
||||||
|
Your one instance of card 6 (one original) has no matching numbers and wins 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, and 1 instance of card 6. In total, this example pile of scratchcards causes you to ultimately have 30 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?
|
||||||
Reference in New Issue
Block a user