-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.py
41 lines (33 loc) · 1.06 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
from datetime import datetime
import re
from functools import reduce
puzzleInput = open("inputs/day04.txt", "r").read().split('\n')
start_time = datetime.now()
puzzleLength = len(puzzleInput) -1
pattern = re.compile("\d+")
result = 0
for line in puzzleInput:
card = line.split(": ")[1].split("|")
hits = set(pattern.findall(card[0])).intersection(pattern.findall(card[1]))
if hits:
result += pow(2, len(hits) - 1)
end_time = datetime.now()
print("1st total is: " + str(result))
print(end_time-start_time)
start_time = datetime.now()
result = 0
pattern = re.compile("\d+")
wonCards = {}
y = 0
while y < len(puzzleInput):
card = puzzleInput[y].split(": ")[1].split("|")
hits = set(pattern.findall(card[0])).intersection(pattern.findall(card[1]))
wonCards[y] = wonCards.get(y, 1)
if hits:
for i in range(y + 1, y + len(hits) + 1):
wonCards[i] = wonCards.get(i, 1) + wonCards[y]
y+= 1
result = reduce(lambda x,y:x+y, wonCards.values())
end_time = datetime.now()
print("2nd total is: " + str(result))
print(end_time-start_time)