From 8d2b7be327c5fde2441a6281d49d9a896e0bb9f3 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 20 Dec 2022 13:45:09 +0100 Subject: [PATCH] 2022: d20: ex2: add solution --- 2022/d20/ex2/ex2.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100755 2022/d20/ex2/ex2.py diff --git a/2022/d20/ex2/ex2.py b/2022/d20/ex2/ex2.py new file mode 100755 index 0000000..e6accd4 --- /dev/null +++ b/2022/d20/ex2/ex2.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python + +import sys +from collections import deque + + +def solve(input: list[int]) -> int: + def mix(file: list[int], rounds: int) -> list[int]: + shuffled = deque(enumerate(file)) + + for _ in range(rounds): + for i, n in enumerate(file): + item = shuffled.index((i, n)) + shuffled.remove((i, n)) + # Moving the item to the left means moving the collection to the left + shuffled.rotate(-n) + shuffled.insert(item, (i, n)) + + return [n for _, n in shuffled] + + def coordinates(file: list[int]) -> tuple[int, int, int]: + zero_index = file.index(0) + indices = map(lambda n: n + zero_index, [1000, 2000, 3000]) + return tuple(map(lambda n: file[n % len(file)], indices)) # type: ignore + + file = [n * 811589153 for n in input] + file = mix(file, 10) + return sum(coordinates(file)) + + +def main() -> None: + input = [int(n) for n in sys.stdin.readlines()] + print(solve(input)) + + +if __name__ == "__main__": + main()