From 74657f9c7374be860d3621794e6c7d4292ea2d55 Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Sun, 9 Dec 2018 14:36:34 +0100 Subject: [PATCH] Add day 9 --- Day9/Day9.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Day9/Day9.py diff --git a/Day9/Day9.py b/Day9/Day9.py new file mode 100644 index 0000000..e684143 --- /dev/null +++ b/Day9/Day9.py @@ -0,0 +1,30 @@ +from sys import argv, exit +from blist import blist + +try: + PLAYER_COUNT = int(argv[1]) + LAST_MARBLE = int(argv[2]) +except (IndexError, ValueError): + print(f"Usage: {argv[0]} [player_count] [marble_count]") + exit(1) + +marbles = blist([0, 2, 1]) +current_id = 1 + +scores = [0] * PLAYER_COUNT +player = 2 + +for next_marble in range(3, LAST_MARBLE + 1): + if next_marble % 23 == 0: + current_id = (current_id - 7) % len(marbles) + scores[player] += next_marble + marbles.pop(current_id) + else: + current_id += 2 + if current_id > len(marbles): + current_id %= len(marbles) + + marbles.insert(current_id, next_marble) + + player = (player + 1) % PLAYER_COUNT + +print(max(scores))