From c2f1db90d636becd602d183a8877b0136ed79e70 Mon Sep 17 00:00:00 2001 From: Thibaud Date: Sat, 5 Dec 2020 11:49:49 +0100 Subject: [PATCH] day5 challenge --- day5/day5.py | 60 ++++ day5/input.txt | 757 +++++++++++++++++++++++++++++++++++++++++++++++++ day5/tests.py | 22 ++ 3 files changed, 839 insertions(+) create mode 100644 day5/day5.py create mode 100644 day5/input.txt create mode 100644 day5/tests.py diff --git a/day5/day5.py b/day5/day5.py new file mode 100644 index 0000000..eacc64f --- /dev/null +++ b/day5/day5.py @@ -0,0 +1,60 @@ +#! /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') diff --git a/day5/input.txt b/day5/input.txt new file mode 100644 index 0000000..2f95cdb --- /dev/null +++ b/day5/input.txt @@ -0,0 +1,757 @@ +FBBBBBBLRR +FFFBFFFLLR +FFBBBBBRRL +BFFBFFBRLL +BFBFBBFLLR +FBFBBFBLLL +FB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+FFBBBBFRRL +FFBBBFBRLR +BFFBFBFRLL +BFFFBBFRRR +FFBFBFFRRR +BFFBBBBRLL \ No newline at end of file diff --git a/day5/tests.py b/day5/tests.py new file mode 100644 index 0000000..489299c --- /dev/null +++ b/day5/tests.py @@ -0,0 +1,22 @@ +#! /usr/bin/env python3 +from day6 import parse_boarding_pass, get_seat_id + +def tests(): + inputs = { + "FBFBBFFRLR": (44, 5, 357), + "BFFFBBFRRR": (70, 7, 567), + "FFFBBBFRRR": (14, 7, 119), + "BBFFBBFRLL": (102, 4, 820) + } + + for boarding_pass, expected in inputs.items(): + row, col = parse_boarding_pass(boarding_pass) + seat_id = get_seat_id(row, col) + assert row == expected[0] + assert col == expected[1] + assert seat_id == expected[2] + print(row, col, seat_id, expected) + + +if __name__ == "__main__": + tests()