diff options
Diffstat (limited to '2020/day5/boarding.py')
-rw-r--r-- | 2020/day5/boarding.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/2020/day5/boarding.py b/2020/day5/boarding.py new file mode 100644 index 0000000..336cd63 --- /dev/null +++ b/2020/day5/boarding.py | |||
@@ -0,0 +1,52 @@ | |||
1 | import math | ||
2 | |||
3 | seat_ids = list() | ||
4 | |||
5 | with open("input", "r") as passes: | ||
6 | |||
7 | for seat in passes: | ||
8 | front = 0 | ||
9 | back = 127 | ||
10 | |||
11 | left = 0 | ||
12 | right = 7 | ||
13 | |||
14 | for i in seat: | ||
15 | if i == "F": | ||
16 | back = math.floor((front + back) / 2) | ||
17 | elif i == "B": | ||
18 | front = math.ceil((front + back) / 2) | ||
19 | elif i == "L": | ||
20 | right = math.floor((right + left) / 2) | ||
21 | else: | ||
22 | left = math.ceil((right + left) / 2) | ||
23 | |||
24 | assert front == back | ||
25 | assert left == right | ||
26 | |||
27 | row = front | ||
28 | col = left | ||
29 | |||
30 | seat_id = row * 8 + col | ||
31 | |||
32 | seat_ids.append(seat_id) | ||
33 | |||
34 | seat_ids.sort() | ||
35 | |||
36 | # answer to the first part | ||
37 | print(seat_ids[-1]) | ||
38 | |||
39 | # answer to the second part | ||
40 | lent = len(seat_ids) | ||
41 | left = 0 | ||
42 | right = lent - 1 | ||
43 | cursor = 0 | ||
44 | |||
45 | while right - left > 1: | ||
46 | cursor = math.ceil((left + right) / 2) | ||
47 | if (seat_ids[left] - left) != (seat_ids[cursor] - cursor): | ||
48 | right = cursor | ||
49 | elif (seat_ids[right] - right) != (seat_ids[cursor] - cursor): | ||
50 | left = cursor | ||
51 | |||
52 | print(seat_ids[cursor] + 1) | ||