-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.py
executable file
·42 lines (33 loc) · 1.17 KB
/
day11.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
#!/usr/local/bin/python3
from collections import defaultdict
from copy import copy
def do_blink(stone_counts):
current_stones = copy(stone_counts)
for stone, count in current_stones.items():
if count == 0:
continue
stone_str = f"{stone}"
if stone == 0:
stone_counts[0] -= count
stone_counts[1] += count
elif len(stone_str) % 2 == 0:
l = len(stone_str)
first = int(stone_str[0 : int(l / 2)])
second = int(stone_str[int(l / 2) :])
stone_counts[first] += count
stone_counts[second] += count
stone_counts[stone] -= count
else:
stone_counts[int(stone) * 2024] += count
stone_counts[stone] -= count
return stone_counts
def day11(stones_str, n):
stone_counts = defaultdict(int)
for val in stones_str.split():
stone_counts[val] = 1
for i in range(1, n + 1):
stone_counts = do_blink(stone_counts)
# print(i, len(stone_counts))
return sum(stone_counts.values())
assert day11("125 17", 25) == 555312
assert day11("77 515 6779622 6 91370 959685 0 9861", 75) == 215933828786931