Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
1.2 kB
1
Indexable
Never
puzzle_input = '.\input.txt'

def main():
    file = open(puzzle_input, 'r')

    seat_ids = list()
    for line in file.readlines():
        seat = translate(line.strip())
        seat_ids.append(seat['id'])

    sorted_ids = sorted(seat_ids)

    missing_ids = set(range(min(sorted_ids), max(sorted_ids))) - set(sorted_ids)
    print(missing_ids)

def translate(boarding_pass: str) -> dict:
    row = bsp(boarding_pass, 0, 7, 0, 127)
    column = bsp(boarding_pass, 7, 10, 0, 7)
    return {
        'row': row,
        'collumn': column,
        'id': row * 8 + column
    }

def bsp(boarding_pass: str, char_start: int, char_end: int, partition_start: int, partition_end: int) -> int:
    letter = boarding_pass[char_start]

    if char_start == char_end - 1:
        if letter == 'F' or letter == 'L':
            return partition_start
        else:
            return partition_end
    partition = (partition_end - partition_start) // 2 + 1
    if letter == 'F' or letter == 'L':
        return bsp(boarding_pass, char_start + 1, char_end, partition_start, partition_end - partition)
    else:
        return bsp(boarding_pass, char_start + 1, char_end, partition_start + partition, partition_end)

main()