summaryrefslogtreecommitdiffstats
path: root/day20/__init__.py
blob: 02ff80d782271cda0ee5c7c3fe58518eda09b85b (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
# -*- 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

        items.insert(new_index, item)

        return items

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

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

        print(
            working_list[1000 % input_size],
            working_list[2000 % input_size],
            working_list[3000 % input_size],
        )

        return sum(
            [
                working_list[1000 % input_size],
                working_list[2000 % input_size],
                working_list[3000 % input_size],
            ]
        )


class AssignmentTwo(Assignment):
    pass