-
Notifications
You must be signed in to change notification settings - Fork 0
/
day05.py
62 lines (41 loc) · 1.35 KB
/
day05.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
from pathlib import Path
import numpy as np
lines = np.genfromtxt('input/' + Path(__file__).stem,
delimiter='', comments=None, dtype='str')
row_count = 128
col_count = 8
def binary_split(str, upper, lower, count):
items = np.arange(0, count)
for char in str:
parts = np.split(items, 2)
if(char == upper):
items = parts[1]
elif(char == lower):
items = parts[0]
assert len(items) == 1
return items[0]
def parse_seat(coord):
row_coord = coord[0:7]
col_coord = coord[7:]
# Determine row
row = binary_split(row_coord, 'B', 'F', row_count)
# Determine col
col = binary_split(col_coord, 'R', 'L', col_count)
return (row, col)
def seat_id(seat_tuple):
return seat_tuple[0] * 8 + seat_tuple[1]
seats = [parse_seat(coord) for coord in lines]
seat_ids = [seat_id(seat) for seat in seats]
# Test
assert parse_seat("BFFFBBFRRR") == (70, 7)
assert seat_id((70, 7)) == 567
print(np.sort(seat_ids))
# Part 2
p2_seats = [seat for seat in seats if seat[0]
!= 0 and seat[0] != row_count - 1]
p2_seat_ids = np.sort([seat_id(seat) for seat in p2_seats])
p2_min = np.min(p2_seat_ids)
p2_max = np.max(p2_seat_ids)
all_seats = np.arange(p2_min, p2_max + 1)
print(f'Part 1: {max(seat_ids)}')
print(f'Part 2: {np.setdiff1d(all_seats, p2_seat_ids)[0]}')