diff options
author | Yigit Sever | 2021-12-13 10:38:11 +0300 |
---|---|---|
committer | Yigit Sever | 2021-12-13 10:38:11 +0300 |
commit | 74b27ccca31bb757c737dd7fdc02f513f57561b2 (patch) | |
tree | e27db4cd0873c81a53d32277446d926d176304e0 /2020/day5/boarding.py | |
parent | 3919f90cfbfbba26c8e39f979280649f5e08aea8 (diff) | |
parent | ac8125750abed263619da4cc6d653bb5ab76f007 (diff) | |
download | aoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.tar.gz aoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.tar.bz2 aoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.zip |
Merge remote-tracking branch 'origin/main'
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) | ||