aboutsummaryrefslogtreecommitdiffstats
path: root/2020/day05/binary-bording.py
diff options
context:
space:
mode:
authorBryan Brattlof <hello@bryanbrattlof.com>2020-12-13 16:43:31 -0500
committerBryan Brattlof <hello@bryanbrattlof.com>2020-12-13 16:43:31 -0500
commitd491af6d5534348450de3be4a017396656a5f9a0 (patch)
tree59565d1b78032ac94f0fbac97e54818fb961dca4 /2020/day05/binary-bording.py
parent2755dd89427c9a35687b9ff392696ea770fadfb8 (diff)
downloadadvent-of-code-d491af6d5534348450de3be4a017396656a5f9a0.tar.gz
advent-of-code-d491af6d5534348450de3be4a017396656a5f9a0.tar.bz2
day 5.1 of Advent of Code 2020
Diffstat (limited to '2020/day05/binary-bording.py')
-rw-r--r--2020/day05/binary-bording.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/2020/day05/binary-bording.py b/2020/day05/binary-bording.py
new file mode 100644
index 0000000..d3cb284
--- /dev/null
+++ b/2020/day05/binary-bording.py
@@ -0,0 +1,43 @@
+
+from typing import NamedTuple
+
+EXAMPLES = [
+ ('FBFBBFFRLR', 44, 5, 357),
+ ('BFFFBBFRRR', 70, 7, 567),
+ ('FFFBBBFRRR', 14, 7, 119),
+ ('BBFFBBFRLL', 102, 4, 820)
+]
+
+class Seat(NamedTuple):
+ row: int
+ seat: int
+
+ @property
+ def seat_id(self) -> int:
+ return self.row * 8 + self.seat
+
+def boarding(seat: str) -> Seat:
+ """given a string return the seat number"""
+ row = 0
+ for i, c in enumerate(reversed(seat[:7])):
+ row += 1 << i * {'B': 1, 'F': 0}[c]
+ row -= 1 * seat.count('F')
+
+ col = 0
+ for i, c in enumerate(reversed(seat[-3:])):
+ col += 1 << i * {'L': 0, 'R': 1}[c]
+ col -= 1 * seat.count('L')
+
+ return Seat(row, col)
+
+for eg, row, col, sid in EXAMPLES:
+ s = Seat(*boarding(eg))
+ assert row == s.row, f'{eg}: {row} != {s.row}'
+ assert col == s.seat, f'{eg}: {col} != {s.seat}'
+ assert sid == s.seat_id, f'{eg}: {sid} != {s.seat_id}'
+
+with open('data/boarding-passes.txt') as f:
+ seats = (boarding(bp.strip()) for bp in f)
+ hi = max(bp.seat_id for bp in seats)
+
+print(f'biggest seat: {hi}')