-
Notifications
You must be signed in to change notification settings - Fork 0
/
2023 D4.py
31 lines (27 loc) · 916 Bytes
/
2023 D4.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
testing = False
f = open(f'inputs/{"samples" if testing else ""}/day4.txt').read().splitlines()
def part1():
sum = 0
for line in f:
exp = 0
winningNums = line.split(' | ')[0].split(': ')[1].split()
myNums = line.split(' | ')[1].split()
for num in myNums:
if num in winningNums: exp += 1
sum += (0 if exp == 0 else 2**(exp-1))
return sum
def part2():
mults = [1 for i in range(len(f))]
for line in f:
won = 1
winningNums = line.split(' | ')[0].split(': ')[1].split()
myNums = line.split(' | ')[1].split()
game = int(line.split(':')[0].split('Card ')[1])-1
for num in myNums:
if num in winningNums:
won += 1
for i in range(game+1, game+won):
mults[i] += mults[game]
return sum(mults)
print('Part 1 Answer:', part1())
print('Part 2 Answer:', part2())