diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-06 05:56:24 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-06 05:56:24 +0000 |
commit | 0689273df8a8220b2ef9d6cc67de9b005392c7ff (patch) | |
tree | cec7769521e6b5e33b4b6be89572b0485cab6a6e | |
parent | 7d710ad3f92e8e85645b7a68b353d2b853bf111e (diff) | |
download | aoc2021-0689273df8a8220b2ef9d6cc67de9b005392c7ff.tar.gz aoc2021-0689273df8a8220b2ef9d6cc67de9b005392c7ff.tar.xz aoc2021-0689273df8a8220b2ef9d6cc67de9b005392c7ff.zip |
day 6: nonrecursive solution
-rw-r--r-- | 6.py | 17 |
1 files changed, 10 insertions, 7 deletions
@@ -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)) |