Cleaned up part 01 and made more pythonic
This commit is contained in:
parent
f1aaf26e86
commit
808b4414c6
|
|
@ -40,7 +40,7 @@ Take a seat in the large pile of colorful cards.
|
|||
<details><summary>How many points are they worth in total?</summary>
|
||||
<p></p>
|
||||
<p>
|
||||
Your puzzle answer was <code>XXXXX</code>.
|
||||
Your puzzle answer was <code>21485</code>.
|
||||
|
||||
The first half of this puzzle is complete! It provides one gold star: *
|
||||
</p>
|
||||
|
|
@ -48,11 +48,39 @@ The first half of this puzzle is complete! It provides one gold star: *
|
|||
|
||||
## 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.
|
||||
|
||||
<details><summary>--------</summary>
|
||||
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,
|
||||
|
||||
<details><summary>how many total scratchcards do you end up with?</summary>
|
||||
<p></p>
|
||||
<p>
|
||||
Your puzzle answer was <code>75220503</code>.
|
||||
Your puzzle answer was <code>XXXXXXXXX</code>.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||
</p>
|
||||
|
|
|
|||
|
|
@ -21,35 +21,39 @@ class Card:
|
|||
cards = []
|
||||
total_points = 0
|
||||
|
||||
print("Parsing Input:")
|
||||
print("")
|
||||
print("===================================\n");
|
||||
#print("Parsing Input:")
|
||||
#print("")
|
||||
#print("===================================\n");
|
||||
|
||||
FILENAME = "/home/icaema/Public/advent-of-code/day-04/data/p01data.txt"
|
||||
f = open(FILENAME, "r")
|
||||
|
||||
#for line in sys.stdin:
|
||||
for line in f.readlines():
|
||||
line = line.strip().replace(' ', ' ').replace('Card ', '').replace(' | ', '|')
|
||||
#card_side
|
||||
card_number = int(line.split(':')[0])
|
||||
[winning_numbers,my_numbers] = line.split(':')[1].split('|')
|
||||
line = line[5:-1].replace(' ', ' ')
|
||||
#debug_print("input", line)
|
||||
|
||||
winning_numbers = set([int(wn) for wn in winning_numbers.strip().split(' ')])
|
||||
my_numbers = [int(mn) for mn in my_numbers.strip().split(' ')]
|
||||
[card_number_side, given_number_side] = line.split(':')
|
||||
|
||||
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 = set([int(wn) for wn in winning_numbers[1:-1].split(' ')])
|
||||
my_numbers = [int(mn) for mn in my_numbers[1:].split(' ')]
|
||||
|
||||
total_points += get_points(len([x for x in my_numbers if x in winning_numbers]))
|
||||
|
||||
current_card = Card(card_number, winning_numbers, my_numbers)
|
||||
debug_print("input", line)
|
||||
debug_print("parsed", current_card)
|
||||
cards.append(current_card)
|
||||
#debug_print("parsed", current_card)
|
||||
|
||||
|
||||
|
||||
|
||||
print("")
|
||||
print(" Proccessing:")
|
||||
print("===================================\n");
|
||||
#print("")
|
||||
#print(" Proccessing:")
|
||||
#print("===================================\n");
|
||||
# Insert Processing Code
|
||||
|
||||
|
||||
|
|
@ -57,9 +61,9 @@ print("===================================\n");
|
|||
|
||||
|
||||
|
||||
print("")
|
||||
print(" Calculate Answer:")
|
||||
print("===================================\n");
|
||||
#print("")
|
||||
#print(" Calculate Answer:")
|
||||
#print("===================================\n");
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue