summaryrefslogtreecommitdiffstats
path: root/2020/day5/boarding.py
diff options
context:
space:
mode:
authorYigit Sever2021-12-13 10:38:11 +0300
committerYigit Sever2021-12-13 10:38:11 +0300
commit74b27ccca31bb757c737dd7fdc02f513f57561b2 (patch)
treee27db4cd0873c81a53d32277446d926d176304e0 /2020/day5/boarding.py
parent3919f90cfbfbba26c8e39f979280649f5e08aea8 (diff)
parentac8125750abed263619da4cc6d653bb5ab76f007 (diff)
downloadaoc-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.py52
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 @@
1import math
2
3seat_ids = list()
4
5with 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
34seat_ids.sort()
35
36# answer to the first part
37print(seat_ids[-1])
38
39# answer to the second part
40lent = len(seat_ids)
41left = 0
42right = lent - 1
43cursor = 0
44
45while 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
52print(seat_ids[cursor] + 1)