-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp11.py
37 lines (27 loc) · 893 Bytes
/
p11.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
from collections import defaultdict
from functools import cache
from aocd import data
@cache
def get_stone(cur_stone):
if len(cur_stone) % 2 == 0:
stone_a = cur_stone[:len(cur_stone)//2]
stone_b = str(int(cur_stone[len(cur_stone)//2:]))
return stone_a, stone_b
elif cur_stone == "0":
return "1", None
else:
return str(int(cur_stone) * 2024), None
stones = defaultdict(int)
for stone in data.split():
stones[stone] += 1
for blink in range(1, 76):
new_stones = defaultdict(int)
for stone, amount in stones.items():
new_stone_a, new_stone_b = get_stone(stone)
new_stones[new_stone_a] += amount
if new_stone_b is not None:
new_stones[new_stone_b] += amount
stones = new_stones
if blink == 25:
print("Part 1:", sum(stones.values()))
print("Part 2:", sum(stones.values()))