summaryrefslogtreecommitdiffstats
path: root/day20/__init__.py
blob: a61253f34ed5665e08f2c856d7a3b4ef2be522b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-
from abc import ABC
from collections import OrderedDict
from typing import Iterator, List

from aoc import BaseAssignment, I, T


class Assignment(BaseAssignment[int, int], ABC):
    def parse_item(self, item: str) -> int:
        return int(item)


class AssignmentOne(Assignment):
    example_result = 3

    @staticmethod
    def move(items: List[int], item: int):
        items = list(items)
        input_size = len(items)

        index = items.index(item)
        item = items.pop(index)

        new_index = (index + item) % input_size

        if item > 0 and index + item >= input_size:
            new_index += 1

        if item < 0 and index + item <= 0:
            new_index -= 1

        if new_index == -1:
            new_index = len(items)

        items.insert(new_index, item)

        return items

    @staticmethod
    def get_nth_number_after_0(items: List[int], n: int):
        index_of_0 = items.index(0)
        return items[(index_of_0 + n) % len(items)]

    def run(self, input: Iterator[I]) -> T:
        input_list = list(input)
        working_list = list(input_list)

        for index, item in enumerate(input_list):
            working_list = self.move(working_list, item)

        return sum(
            [
                self.get_nth_number_after_0(working_list, 1000),
                self.get_nth_number_after_0(working_list, 2000),
                self.get_nth_number_after_0(working_list, 3000),
            ]
        )


class AssignmentTwo(Assignment):
    pass