-
Notifications
You must be signed in to change notification settings - Fork 0
/
advent04.py
executable file
·73 lines (57 loc) · 1.72 KB
/
advent04.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
"""
https://adventofcode.com/2023/day/4
Usage:
cat advent04.input | ./advent04.py
"""
import sys
import re
from dataclasses import dataclass
def main ():
cards = parse_input(sys.stdin.read().strip().split('\n'))
print("Part 1:", part1(cards))
# 24542
print("Part 2:", part2(cards))
# 8736438
def part1 (cards):
total_sum = 0
for card in cards:
card_sum = 0
for num in card.my_numbers:
if num in card.winning_numbers:
if card_sum > 0:
card_sum = card_sum * 2
else:
card_sum = 1
total_sum = total_sum + card_sum
return total_sum
def part2 (cards):
for idx in range(len(cards)):
card_sum = 0
for num in cards[idx].my_numbers:
if num in cards[idx].winning_numbers:
card_sum = card_sum + 1
for add_instances_for_card in range(idx+1, idx+1+card_sum):
cards[add_instances_for_card].instances = cards[add_instances_for_card].instances + cards[idx].instances
total_sum = 0
for card in cards:
total_sum = total_sum + card.instances
return total_sum
def parse_input (input):
cards = []
for line in input:
winning_numbers = []
my_numbers = []
numbers = line.split(':')[1]
left, right = numbers.split('|')
winning_numbers = [int(x) for x in re.split('\s+', left.strip())]
my_numbers = [int(x) for x in re.split('\s+', right.strip())]
cards.append(Card(winning_numbers, my_numbers))
return cards
@dataclass
class Card:
winning_numbers: list[int]
my_numbers: list[int]
instances: int = 1
if __name__ == '__main__':
main()