#!/usr/bin/env python3
"""Template file to be used as boilerplate for each day puzzle."""
from aoc_utilities import Input
import aocd
import os
# import re
import sys
# import itertools
from collections import deque, defaultdict
# 2 digit day fetched from filename
DAY = os.path.basename(__file__)[3:5]


"""
PART1
"""



def solve1(input):
    """Solves part 1."""
    input = input[0].split()
    players = int(input[0])
    max_marble = int(input[6])
    return marble_scores(players, max_marble)


def marble_scores(max_players, last_marble):
    scores = defaultdict(int)
    circle = deque([0])

    for marble in range(1, last_marble + 1):
        if marble % 23 == 0:
            circle.rotate(7)  # Rotate the deque n steps to the right
            scores[marble % max_players] += marble + circle.pop()  # .pop() : Remove and return an element from the right side of the deque.
            circle.rotate(-1)
        else:
            circle.rotate(-1)
            circle.append(marble)  # Add to the right side of the deque.

    return max(scores.values()) if scores else 0

# >>> from collections import deque
# >>> circle = deque([1,2,3,4])

# >>> circle.rotate(1)
# >>> circle
# deque([4, 1, 2, 3])

# >>> circle.pop()
# 3

# >>> circle.append(5)
# >>> circle
# deque([4, 1, 2, 5])


"""
PART 2
"""


def solve2(input):
    """Solves part2."""
    input = input[0].split()
    players = int(input[0])
    max_marble = int(input[6])
    return marble_scores(players, 100 * max_marble)


"""
Use script args to execute the right function.
"""
if __name__ == '__main__':
    if len(sys.argv) > 1 and sys.argv[1] == '1':
        res = solve1((Input(DAY).readlines()))
        print(res)
        if len(sys.argv) == 3:
            if sys.argv[2] == 's':
                print("attempting to submit the response '{}' to part 1: \n\n".format(res))
                aocd.submit1(res)

    if len(sys.argv) > 1 and sys.argv[1] == '2':
        res = solve2((Input(DAY).readlines()))
        print(res)
        if len(sys.argv) == 3:
            if sys.argv[2] == 's':
                print("attempting to submit the response '{}' to part 2: \n\n".format(res))
                aocd.submit2(res)
