-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.py
70 lines (50 loc) · 1.74 KB
/
day5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# %%
from collections import namedtuple
def partition(low: int, up: int, upper: bool) -> tuple:
if upper:
low = int((up + low + 1) / 2)
else:
up = int((up + low - 1) / 2)
return (low, up)
Seat = namedtuple('Seat', ["row", "column", "seatID"])
def find_seat(seat_str: str) -> Seat:
# method 1
upper_or_lower = [char == 'B' for char in seat_str[:7]]
left_or_right = [char == 'R' for char in seat_str[7:10]]
row_min, row_max = 0, 127
for i in upper_or_lower:
row_min, row_max = partition(row_min, row_max, i)
assert row_min == row_max
col_min, col_max = 0, 7
for i in left_or_right:
col_min, col_max = partition(col_min, col_max, i)
assert col_min == col_max
# method 2: converts binary to decimal
row_min = int(''.join(['1' if char == 'B' else '0' for char in seat_str[:7]]), 2)
col_min = int(''.join(['1' if char == 'R' else '0' for char in seat_str[7:10]]), 2)
return Seat(row=row_min, column=col_min, seatID=row_min * 8 + col_min)
input = """BFFFBBFRRR
FFFBBBFRRR
BBFFBBFRLL
"""
# : row 70, column 7, seat ID 567.
# : row 14, column 7, seat ID 119.
# : row 102, column 4, seat ID 820
with open('inputs/5.txt', 'r') as file:
input = file.read()
# seats = [find_seat(seat_str) for seat_str in file]
seats = [find_seat(seat_str) for seat_str in input.splitlines()]
print(max(seat.seatID for seat in seats))
# %% Method 1
import pandas as pd
x = pd.DataFrame(seats)
print()
y = x.groupby('row').sum().column != 28
print(y[y])
print(x[x.row == 79])
79 * 8 + 4
# %% Method 2
seat_ids = [seat.seatID for seat in seats]
lo = min(seat_ids)
hi = max(seat_ids)
print([x for x in range(lo, hi) if x not in seat_ids and x - 1 in seat_ids and x + 1 in seat_ids])