-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_a.py
executable file
·78 lines (65 loc) · 2.18 KB
/
part_a.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
#!/usr/bin/env python3
from collections import namedtuple
import utils
class Challenge(utils.BaseChallenge):
def solve(self, _input, debug=False):
"""
>>> Challenge().default_solve()
842
"""
return BoardingPasses.from_passes_text(_input).get_highest_seat_id()
class BoardingPass(namedtuple("BoardingPass", ("name",))):
@classmethod
def from_pass_text(cls, pass_text):
"""
>>> BoardingPass.from_pass_text("FBFBBFFRLR")
BoardingPass(name='FBFBBFFRLR')
"""
return cls(pass_text.strip())
def get_seat_id(self):
"""
>>> BoardingPass("FBFBBFFRLR").get_seat_id()
357
>>> BoardingPass("BFFFBBFRRR").get_seat_id()
567
>>> BoardingPass("FFFBBBFRRR").get_seat_id()
119
>>> BoardingPass("BBFFBBFRLL").get_seat_id()
820
"""
binary_id_str = self.name\
.replace("F", "0")\
.replace("B", "1")\
.replace("L", "0")\
.replace("R", "1")
return int(binary_id_str, base=2)
class BoardingPasses:
boarding_pass_class = BoardingPass
@classmethod
def from_passes_text(cls, passes_text):
"""
>>> BoardingPasses.from_passes_text(
... "FBFBBFFRLR\\n"
... "BFFFBBFRRR\\n"
... "FFFBBBFRRR\\n"
... "BBFFBBFRLL\\n"
... ).passes
[BoardingPass(name='FBFBBFFRLR'), BoardingPass(name='BFFFBBFRRR'), BoardingPass(name='FFFBBBFRRR'), BoardingPass(name='BBFFBBFRLL')]
"""
lines = passes_text.splitlines()
non_empty_lines = filter(None, lines)
return cls(list(map(
cls.boarding_pass_class.from_pass_text, non_empty_lines)))
def __init__(self, passes):
self.passes = passes
def get_highest_seat_id(self):
"""
>>> BoardingPasses([
... BoardingPass('FBFBBFFRLR'), BoardingPass('BFFFBBFRRR'),
... BoardingPass('FFFBBBFRRR'), BoardingPass('BBFFBBFRLL')])\\
... .get_highest_seat_id()
820
"""
return max(map(self.boarding_pass_class.get_seat_id, self.passes))
Challenge.main()
challenge = Challenge()