Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_puzzle_data(a_file_name: str) -> list[str]:
- with open(a_file_name) as INFILE:
- return [line.rstrip() for line in INFILE]
- def binary_split(a_front_or_back: str, a_lo: int, a_hi: int) -> tuple[int, int]:
- new_lo: int
- new_hi: int
- range_length = a_hi - a_lo + 1
- if a_front_or_back in 'fFlL':
- new_lo = a_lo
- new_hi = a_lo + (range_length // 2) - 1
- elif a_front_or_back in 'bBrR':
- new_lo = a_lo + (range_length // 2)
- new_hi = a_hi
- return new_lo, new_hi
- def get_seating_id(a_boarding_pass: str) -> int:
- seat_id: int = 0
- lo, hi = 0, 127
- for split_type in a_boarding_pass[0:7]:
- lo, hi = binary_split(split_type, lo, hi)
- assert lo == hi, 'Er...'
- seat_row = lo
- # print(f"Row={lo}")
- lo, hi = 0, 7
- for split_type in a_boarding_pass[7:]:
- lo, hi = binary_split(split_type, lo, hi)
- assert lo == hi, 'Er...'
- seat_col = lo
- # print(f"Column={lo}")
- return (8 * seat_row) + seat_col
- def get_missing_seat_id(a_boarding_passes: list[str]) -> int:
- missing_seat_id: int = 0
- boarding_pass_seat_ids = [get_seating_id(boarding_pass) for boarding_pass in a_boarding_passes]
- boarding_pass_seat_ids.sort()
- missing_seat_id = [
- seat_id
- for seat_id in boarding_pass_seat_ids
- if ((seat_id + 1 not in boarding_pass_seat_ids
- and seat_id != boarding_pass_seat_ids[-1]))
- ]
- return missing_seat_id[0] + 1
- boarding_passes = get_puzzle_data('aoc_2020/aoc_2020_day_05.txt')
- # print(f'{boarding_passes=}')
- print(f"p1={max([get_seating_id(boarding_pass) for boarding_pass in boarding_passes])}")
- print(f"p2={get_missing_seat_id(boarding_passes)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement