-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day4.py
45 lines (40 loc) · 1.48 KB
/
Day4.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
import re
import numpy as np
def part2():
noc = sum(1 for _ in open('inputs/day4.txt'))
numOfCards = np.ones(noc)
with open('inputs/day4.txt') as file:
for index,line in enumerate(file):
counter = 0
_,card = line.split(":")
winning, have = card.split("|")
winning_numbers = re.findall(pattern='\d+', string=winning)
current_numbers = re.findall(pattern='\d+', string=have)
for number in current_numbers:
for win in winning_numbers:
if number == win:
counter += 1
break
#print("index "+ str(index)+" counter "+ str(counter))
#print(numOfCards)
for j in range(index+1,min(index+counter+1,noc)):
numOfCards[j] += numOfCards[index]
return sum(numOfCards)
def part1():
total = 0
with open('inputs/day4.txt') as file:
for line in file:
counter = -1
_,card = line.split(":")
winning, have = card.split("|")
winning_numbers = re.findall(pattern='\d+', string=winning)
current_numbers = re.findall(pattern='\d+', string=have)
for number in current_numbers:
for win in winning_numbers:
if number == win:
counter += 1
break
if counter > -1:
total += 2**counter
return total
print(part2())