summaryrefslogtreecommitdiffstats
path: root/day5/__init__.py
blob: 405b589a916c7969b629577c0ec9bb49c3dedccf (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
from math import ceil, floor
from typing import Iterator, Any

from aoc import BaseAssignment


class Assignment(BaseAssignment):
    def parse_item(self, item: str) -> Any:
        row = self.calculate_postition(input=item[:7], min=0, max=127)
        col = self.calculate_postition(input=item[7:], min=0, max=7)
        return (row * 8) + col

    def calculate_postition(self, input: str, min: int, max: int) -> int:
        input_length = len(input)
        action = input[0]
        half = (max - min) / 2
        if action in ['F', 'L']:
            return min \
                if input_length == 1 \
                else self.calculate_postition(input[1:], min=min,
                                              max=floor(max - half))
        elif action in ['B', 'R']:
            return max \
                if input_length == 1 \
                else self.calculate_postition(input[1:], min=ceil(min + half),
                                              max=max)


class AssignmentOne(Assignment):
    def run(self, input: Iterator) -> Any:
        return max(*input)


class AssignmentTwo(Assignment):
    def run(self, input: Iterator) -> Any:
        seat_ids = sorted(input)
        for index, id in enumerate(seat_ids):
            next_id = id + 1
            if id + 1 != seat_ids[index + 1]:
                return next_id