2017: d06: ex1: add solution
This commit is contained in:
parent
1b36307f94
commit
fd4e28c410
1 changed files with 40 additions and 0 deletions
40
2017/d06/ex1/ex1.py
Executable file
40
2017/d06/ex1/ex1.py
Executable file
|
|
@ -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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue