-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.py
73 lines (53 loc) · 1.47 KB
/
day04.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
from common import *
def getMatchCount(card):
card = card.split(": ")
card.pop(0)
card = "".join(card) # join the list back into a string
halves = card.split("|")
winningNums = halves[0].split(" ")
ourNums = halves[1].split(" ")
# remove empty things
winningNums = [x for x in winningNums if x != ' ' and x != '']
ourNums = [x for x in ourNums if x != ' ' and x != '']
matches = 0
for num in winningNums:
if num in ourNums:
matches += 1
return matches
def part1():
lines = getDayInput(4, 1)
totalPoints = []
for card in lines:
matches = getMatchCount(card)
if matches == 0:
score = 0
elif matches == 1:
score = 1
else:
score = 1
# double for each match
for _ in range(0, matches-1):
score = score * 2
totalPoints.append(score)
# print(winningNums, ourNums, testMatched, matches, score)
return sum(totalPoints)
def part2():
lines = getDayInput(4, 2)
scratchcards = []
for card in lines:
scratchcards.append({
"matches": getMatchCount(card),
"count": 1
})
for i in range(0, len(scratchcards)):
count, matches = scratchcards[i]["count"], scratchcards[i]["matches"]
for _ in range(0, count): # for however many of this card we have
# we get multiple of the next X cards depending on how many matches we have
for j in range(i+1, i+1+matches):
scratchcards[j]["count"] += 1
total = 0
for card in scratchcards:
total += card["count"]
return total
print(f"Part 1: {part1()}")
print(f"Part 2: {part2()}")