-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday4.py
More file actions
64 lines (59 loc) · 1.48 KB
/
day4.py
File metadata and controls
64 lines (59 loc) · 1.48 KB
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
from functools import cache
def star1():
sum = 0
for line in contents:
total = 0
line = line.split(":")[1]
line = line.split("|")
vals = line[0].split(" ")
while "" in vals:
vals.remove("")
cards = line[1].split(" ")
while "" in cards:
cards.remove("")
for v in cards:
if v in vals:
if total == 0:
total = 1
else:
total *= 2
print(total)
sum += total
return sum
copies = dict()
def star2():
sum = 0
for line in contents:
total = 0
id = line.split(":")[0]
id = int(id.split(' ')[-1])
line = line.split(":")[1]
line = line.split("|")
vals = line[0].split(" ")
while "" in vals:
vals.remove("")
cards = line[1].split(" ")
while "" in cards:
cards.remove("")
for v in cards:
if v in vals:
if total == 0:
total = 1
else:
total += 1
copies[id] = [id + i for i in range(1, total+1)]
total = 0
for id in range(1, len(contents) + 1):
total += (helper(id))
return total
@cache
def helper(id):
total = 1
if len(copies[id]) == 0:
return 1
for v in copies[id]:
total += helper(v)
return total
contents = open("day4.txt").read().splitlines()
print(star1())
print(star2())