-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.py
50 lines (37 loc) · 1.38 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
46
47
48
49
50
from advent import elf
# TIL: glad I remembered my septoi helper to deal with split() giving empty strings, ugh.
# As with fibonacci, building bottom up ~ dynamic programming ~ works best
def main():
test_lines = elf.read_lines(__file__, test=True)
lines = elf.read_lines(__file__)
print("Part 1 (test):")
print(part1(test_lines))
print("Part 1:")
print(part1(lines))
print("Part 2 (test):")
print(part2(test_lines))
print("Part 2:")
print(part2(lines))
def part1(lines):
return sum(score(line) for line in lines)
def winners(have, winning):
return len(set(have) & set(winning))
def score(line):
_, winning, have = parse(line)
n = winners(winning, have)
return 2 ** (n - 1) if n >= 1 else 0
def parse(line):
left_card_id, right_nums = [x.strip() for x in line.split(":")]
card_id = elf.septoi(left_card_id)[1]
winning, have = [elf.septoi(x.strip()) for x in right_nums.split("|")]
return card_id, winning, have
def part2(lines):
res = {} # card-id: # of cards fully generated
for line in reversed(lines): # building bottom-up
card_id, winning, have = parse(line)
n = winners(have, winning)
copies = sum(res[copy_id] for copy_id in range(card_id + 1, card_id + 1 + n))
res[card_id] = copies + 1 # plus original
return sum(res.values())
if __name__ == '__main__':
main()