#! /usr/bin/env python3 from itertools import product def main(filename): results = {} with open(filename) as inp: for line in inp: boarding_pass = line.rstrip() row, col = parse_boarding_pass(boarding_pass) seat_id = get_seat_id(row, col) results[boarding_pass] = (row, col, seat_id) part1(results) part2(results) def part1(results): # part 1 max_seat_id = max(x[2] for x in results.values()) print("Max seat ID: ", max_seat_id) def part2(results): seat_ids = sorted(x[2] for x in results.values()) missing_seat_ids = set(range(max(seat_ids))) - set(seat_ids) print(missing_seat_ids) def parse_boarding_pass(boarding_pass): row = _parse(boarding_pass[:7], "F", "B", 128) col = _parse(boarding_pass[7:], "L", "R", 7) return row, col def _parse(inp, lower_option, upper_option, number): rows = slice(0, number - 1) for l in inp: if l == lower_option: rows = lower_half(rows) else: rows = upper_half(rows) return rows.start def lower_half(sl: slice): length = sl.stop - sl.start return slice(sl.start, sl.start + length // 2) def upper_half(sl: slice): length = sl.stop - sl.start return slice(1 + sl.start + length // 2, sl.stop) def get_seat_id(row, col): return 8 * row + col if __name__ == "__main__": main('input.txt')