-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_five.py
103 lines (75 loc) · 2.76 KB
/
day_five.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
class Range:
def __init__(self, min, max):
self.min = min
self.max = max
@property
def diff(self):
return abs(self.min - self.max) + 1
def __str__(self):
return "{} - {}".format(self.min, self.max)
class Seat:
def __init__(self, row, col):
self.row = row
self.col = col
@property
def seat_id(self):
id_row_multiplication = 8
return self.row * id_row_multiplication+ self.col
def row_exists(self):
first_row = 0
last_row = 127
return not(self.row == first_row or self.row == last_row)
def __str__(self):
return "{} - {}".format(self.row, self.col)
class Plane:
def __init__(self):
self.available_seats = [[j*8+i for i in range(0, 8)] for j in range(0, 128)]
def book_seat(self, seat):
self.available_seats[seat.row].remove(seat.seat_id)
@property
def single_seat_available(self):
str = ""
count = 0
for row in self.available_seats:
if len(row) == 1:
str += "row({}): seat_id({}) ({})\n".format(count, row, len(row))
count += 1
return str
def __str__(self):
str = ""
count = 0
for row in self.available_seats:
if len(row) > 0:
str += "row({}): seat_ids({}) ({})\n".format(count, row, len(row))
count += 1
return str
def recursive_determine_seat(seat_string, seat_rows, seat_cols):
code = seat_string[0]
if code == "B":
seat_rows.min = seat_rows.min + (seat_rows.diff / 2)
elif code == "F":
seat_rows.max = seat_rows.max - (seat_rows.diff / 2)
elif code == "R":
seat_cols.min = seat_cols.min + (seat_cols.diff / 2)
elif code == "L":
seat_cols.max = seat_cols.max - (seat_cols.diff / 2)
if len(seat_string) == 1:
return seat_rows.min, seat_cols.min
else:
remaining_code = seat_string[1:]
return recursive_determine_seat(remaining_code, seat_rows, seat_cols)
def determine_highest_seat(seats_string_list):
highest_seat_location = 0
for seat_string in seats_string_list:
seat_location = recursive_determine_seat(seat_string, Range(0, 127), Range(0, 7))
seat_calculation = seat_location[0] * 8 + seat_location[1]
if seat_calculation > highest_seat_location:
highest_seat_location = seat_calculation
return highest_seat_location
def determine_my_seat(seats_string_list):
plane = Plane()
for seat_string in seats_string_list:
seat_location = recursive_determine_seat(seat_string, Range(0, 127), Range(0, 7))
seat = Seat(row=int(seat_location[0]), col=int(seat_location[1]))
plane.book_seat(seat)
return plane.single_seat_available