summaryrefslogtreecommitdiffstats
path: root/day5/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'day5/__init__.py')
-rw-r--r--day5/__init__.py40
1 files changed, 40 insertions, 0 deletions
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 @@
1from math import ceil, floor
2from typing import Iterator, Any
3
4from aoc import BaseAssignment
5
6
7class Assignment(BaseAssignment):
8 def parse_item(self, item: str) -> Any:
9 row = self.calculate_postition(input=item[:7], min=0, max=127)
10 col = self.calculate_postition(input=item[7:], min=0, max=7)
11 return (row * 8) + col
12
13 def calculate_postition(self, input: str, min: int, max: int) -> int:
14 input_length = len(input)
15 action = input[0]
16 half = (max - min) / 2
17 if action in ['F', 'L']:
18 return min \
19 if input_length == 1 \
20 else self.calculate_postition(input[1:], min=min,
21 max=floor(max - half))
22 elif action in ['B', 'R']:
23 return max \
24 if input_length == 1 \
25 else self.calculate_postition(input[1:], min=ceil(min + half),
26 max=max)
27
28
29class AssignmentOne(Assignment):
30 def run(self, input: Iterator) -> Any:
31 return max(*input)
32
33
34class AssignmentTwo(Assignment):
35 def run(self, input: Iterator) -> Any:
36 seat_ids = sorted(input)
37 for index, id in enumerate(seat_ids):
38 next_id = id + 1
39 if id + 1 != seat_ids[index + 1]:
40 return next_id