From cfb24fc103289fd73d9773b39a6d38d98070f9c9 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 19 May 2025 21:31:46 +0100 Subject: [PATCH] 2016: d16: ex2: add solution --- 2016/d16/ex2/ex2.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100755 2016/d16/ex2/ex2.py diff --git a/2016/d16/ex2/ex2.py b/2016/d16/ex2/ex2.py new file mode 100755 index 0000000..74e27ee --- /dev/null +++ b/2016/d16/ex2/ex2.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python + +import itertools +import sys + + +def solve(input: str) -> str: + def curve_step(input: str) -> str: + b = input.translate(str.maketrans("01", "10"))[::-1] + return input + "0" + b + + def checksum(state: str) -> str: + while len(state) % 2 == 0: + state = "".join(str(int(a == b)) for a, b in itertools.batched(state, 2)) + return state + + state = input.strip() + disk_size = 35651584 + while len(state) < disk_size: + state = curve_step(state) + return checksum(state[:disk_size]) + + +def main() -> None: + input = sys.stdin.read() + print(solve(input)) + + +if __name__ == "__main__": + main()