summaryrefslogtreecommitdiffstats
path: root/2020/day5/boarding.py
diff options
context:
space:
mode:
authorYigit Sever2021-12-12 01:24:32 +0300
committerYigit Sever2021-12-12 01:24:32 +0300
commit4bb6f8d06c0e384f3394012b1d48da58ed28cc5e (patch)
treed6478c85c0488a1059567ccd2882cb10039c2546 /2020/day5/boarding.py
parentae3853b6e8ab02023ccd74baac6dc177b1ee879a (diff)
downloadaoc-4bb6f8d06c0e384f3394012b1d48da58ed28cc5e.tar.gz
aoc-4bb6f8d06c0e384f3394012b1d48da58ed28cc5e.tar.bz2
aoc-4bb6f8d06c0e384f3394012b1d48da58ed28cc5e.zip
2020, tracking
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)