summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2021-12-06 05:56:24 +0000
committerTomasz Kramkowski <tk@the-tk.com>2021-12-06 05:56:24 +0000
commit0689273df8a8220b2ef9d6cc67de9b005392c7ff (patch)
treecec7769521e6b5e33b4b6be89572b0485cab6a6e
parent7d710ad3f92e8e85645b7a68b353d2b853bf111e (diff)
downloadaoc2021-0689273df8a8220b2ef9d6cc67de9b005392c7ff.tar.gz
aoc2021-0689273df8a8220b2ef9d6cc67de9b005392c7ff.tar.xz
aoc2021-0689273df8a8220b2ef9d6cc67de9b005392c7ff.zip
day 6: nonrecursive solution
-rw-r--r--6.py17
1 files 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))