From c27aa66e04db1b5280f576655520d67e08ce019e Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 19 May 2025 23:00:08 +0100 Subject: [PATCH] 2016: d19: ex2: add solution --- 2016/d19/ex2/ex2.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 2016/d19/ex2/ex2.py diff --git a/2016/d19/ex2/ex2.py b/2016/d19/ex2/ex2.py new file mode 100755 index 0000000..99f9dcc --- /dev/null +++ b/2016/d19/ex2/ex2.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python + +import collections +import sys + + +def solve(input: str) -> int: + def white_elephant(num_elves: int) -> int: + left = collections.deque(range(1, (num_elves // 2) + 1)) + right = collections.deque(range(num_elves, (num_elves // 2), -1)) + + while left and right: + if len(left) > len(right): + left.pop() + else: + right.pop() + right.appendleft(left.popleft()) + left.append(right.pop()) + return (left + right)[0] + + num_elves = int(input.strip()) + return white_elephant(num_elves) + + +def main() -> None: + input = sys.stdin.read() + print(solve(input)) + + +if __name__ == "__main__": + main()