-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_06.py
48 lines (32 loc) · 1.2 KB
/
day_06.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
from functools import cache
from itertools import groupby
from utils.file_utils import get_input
from utils.measurements import timed
input = get_input().strip()
lanterns = [int(counter) for counter in input.strip().split(",")]
def estimate_fish_population(fish: list[int], days: int = 80, offsprings: list[int] = (7, 9)) -> int:
fish_groups = {k: len(list(v)) for k, v in groupby(sorted(fish))}
@cache
def count_siblings(days_left: int, amount: int, days: int):
if days_left <= days:
return sum(count_siblings(c, amount, days - days_left) for c in offsprings)
return amount
return sum(count_siblings(days_left + 1, amount, days) for days_left, amount in fish_groups.items())
@timed("Part01:")
def part01():
return estimate_fish_population(lanterns)
@timed("Part02:")
def part02():
return estimate_fish_population(lanterns, days=256)
@timed("Part02_Fast:")
def part02_fast(days: int = 256):
groups = [input.count(str(i)) for i in range(9)]
for _ in range(days):
births = groups[0]
groups = groups[1:] + [births]
groups[6] += births
return sum(groups)
if __name__ == '__main__':
part01()
part02()
part02_fast(256)