From 0689273df8a8220b2ef9d6cc67de9b005392c7ff Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Mon, 6 Dec 2021 05:56:24 +0000 Subject: day 6: nonrecursive solution --- 6.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/6.py b/6.py index b290710..2d189de 100644 --- a/6.py +++ b/6.py @@ -1,12 +1,15 @@ -from functools import cache +from collections import Counter, deque from utils import open_day fish = list(map(int, open_day(6).read().split(','))) -@cache -def count_fish(life): - if life < 0: return 1 - return count_fish(life - 7) + count_fish(life - 9) +def solve(fish, life): + d = deque(Counter(fish)[i] for i in range(9)) + for i in range(life): + zeros = d.popleft() + d[6] += zeros + d.append(zeros) + return sum(d) -print(sum(count_fish(79 - f) for f in fish)) -print(sum(count_fish(255 - f) for f in fish)) +print(solve(fish, 80)) +print(solve(fish, 256)) -- cgit v1.2.3-54-g00ecf