From 5cb92a87d99147ee2e7f867ab06d323e413b776c Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sun, 29 Dec 2024 19:40:26 -0500 Subject: [PATCH] 2018: d09: ex1: add solution --- 2018/d09/ex1/ex1.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 2018/d09/ex1/ex1.py diff --git a/2018/d09/ex1/ex1.py b/2018/d09/ex1/ex1.py new file mode 100755 index 0000000..3b409e4 --- /dev/null +++ b/2018/d09/ex1/ex1.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python + +import collections +import sys + + +def solve(input: str) -> int: + def parse(input: str) -> tuple[int, int]: + split = input.split() + return int(split[0]), int(split[6]) + + def play_game(players: int, last_marble: int) -> list[int]: + circle = collections.deque([0]) + scores = [0] * players + + for marble in range(1, last_marble + 1): + if marble % 23 == 0: + scores[marble % players] += marble + circle.rotate(7) + scores[marble % players] += circle.pop() + circle.rotate(-1) + else: + circle.rotate(-1) + circle.append(marble) + + return scores + + players, last_marble = parse(input) + scores = play_game(players, last_marble) + return max(scores) + + +def main() -> None: + input = sys.stdin.read() + print(solve(input)) + + +if __name__ == "__main__": + main()