From 4060ddf2da77441090fa8d097e411670a79b90de Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Sun, 13 Dec 2020 20:23:03 +0100 Subject: Added day 5 --- day5/__init__.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 day5/__init__.py (limited to 'day5/__init__.py') diff --git a/day5/__init__.py b/day5/__init__.py new file mode 100644 index 0000000..405b589 --- /dev/null +++ b/day5/__init__.py @@ -0,0 +1,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 -- cgit v1.2.3