From fb40ea0517a6df2219894102ce6846fd3f71b467 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 6 Dec 2021 12:29:44 +0100 Subject: [PATCH] 2021: d06: ex1: add solution --- 2021/d06/ex1/ex1.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100755 2021/d06/ex1/ex1.py diff --git a/2021/d06/ex1/ex1.py b/2021/d06/ex1/ex1.py new file mode 100755 index 0000000..65abb44 --- /dev/null +++ b/2021/d06/ex1/ex1.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python + +import itertools +import sys +from collections import Counter +from typing import Iterator, List, TypeVar + +T = TypeVar("T") + + +def nth(iterable: Iterator[T], n: int) -> T: + return next(itertools.islice(iterable, n, None)) + + +def solve(input: List[str]) -> int: + fish = [0] * 9 + for n, count in Counter(map(int, input[0].split(","))).items(): + fish[n] = count + + def step(fish: List[int]) -> List[int]: + # Count how many clones happen + new_fish = fish[0] + + # Do the next cycle + fish[0:-1] = fish[1:] + fish[6] += new_fish + fish[8] = new_fish # Override number of new fish + + return fish + + def iter(fish: List[int]) -> Iterator[List[int]]: + while True: + yield (fish := step(fish)) + + return sum(nth(iter(fish), 80 - 1)) + + +def main() -> None: + input = [line.strip() for line in sys.stdin.readlines()] + print(solve(input)) + + +if __name__ == "__main__": + main()