From 4bb6f8d06c0e384f3394012b1d48da58ed28cc5e Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Sun, 12 Dec 2021 01:24:32 +0300 Subject: 2020, tracking --- 2020/day5/boarding.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 2020/day5/boarding.py (limited to '2020/day5/boarding.py') 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 @@ +import math + +seat_ids = list() + +with open("input", "r") as passes: + + for seat in passes: + front = 0 + back = 127 + + left = 0 + right = 7 + + for i in seat: + if i == "F": + back = math.floor((front + back) / 2) + elif i == "B": + front = math.ceil((front + back) / 2) + elif i == "L": + right = math.floor((right + left) / 2) + else: + left = math.ceil((right + left) / 2) + + assert front == back + assert left == right + + row = front + col = left + + seat_id = row * 8 + col + + seat_ids.append(seat_id) + +seat_ids.sort() + +# answer to the first part +print(seat_ids[-1]) + +# answer to the second part +lent = len(seat_ids) +left = 0 +right = lent - 1 +cursor = 0 + +while right - left > 1: + cursor = math.ceil((left + right) / 2) + if (seat_ids[left] - left) != (seat_ids[cursor] - cursor): + right = cursor + elif (seat_ids[right] - right) != (seat_ids[cursor] - cursor): + left = cursor + +print(seat_ids[cursor] + 1) -- cgit v1.2.3-70-g09d2