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