From fd4e28c41036e6e0770c558716270528a14808a2 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 9 May 2025 18:00:24 +0100 Subject: [PATCH] 2017: d06: ex1: add solution --- 2017/d06/ex1/ex1.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100755 2017/d06/ex1/ex1.py diff --git a/2017/d06/ex1/ex1.py b/2017/d06/ex1/ex1.py new file mode 100755 index 0000000..e729e49 --- /dev/null +++ b/2017/d06/ex1/ex1.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python + +import itertools +import sys + +MemoryBlocks = tuple[int, ...] + + +def solve(input: str) -> int: + def parse(input: str) -> MemoryBlocks: + return tuple(int(n) for n in input.split()) + + def redistribute(nums: MemoryBlocks) -> MemoryBlocks: + res = list(nums) + i = res.index(max(res)) # Quick and hasty `argmax` + n, res[i] = res[i], 0 + common, remain = n // len(res), n % len(res) + for j in range(i + 1, i + remain + 1): + res[j % len(res)] += 1 + for j in range(len(res)): + res[j] += common + return tuple(res) + + blocks = parse(input) + seen: set[MemoryBlocks] = set() + for i in itertools.count(): + if blocks in seen: + return i + seen.add(blocks) + blocks = redistribute(blocks) + assert False # Sanity check + + +def main() -> None: + input = sys.stdin.read() + print(solve(input)) + + +if __name__ == "__main__": + main()