diff --git a/day_5/info.md b/day_5/info.md new file mode 100644 index 0000000..2a32170 --- /dev/null +++ b/day_5/info.md @@ -0,0 +1 @@ +https://adventofcode.com/2020/day/5 \ No newline at end of file diff --git a/day_5/part_1.py b/day_5/part_1.py new file mode 100644 index 0000000..479096e --- /dev/null +++ b/day_5/part_1.py @@ -0,0 +1,34 @@ +def binary_partition(instructions, area, lesser): + # Base Case: no instructions remaining + if len(instructions) == 0: + if instructions == lesser: + return area[0] + else: + return area[1] + + # Recursive Case: Multiple instructions remaining + mid = ((area[1] - area[0]) // 2) + area[0] + if instructions[0] == lesser: + return binary_partition(instructions[1:], [area[0], mid], lesser) + else: + return binary_partition(instructions[1:], [mid, area[1]], lesser) + + +def row(instructions): + return binary_partition(instructions, [0, 127], "F") + +def col(instructions): + return binary_partition(instructions, [0, 7], "L") + + +with open("input") as f: + data = f.read().split("\n") + +seat_ids = [] + +for boarding_pass in data: + r = row(boarding_pass[:-3]) + c = col(boarding_pass[-3:]) + seat_id = (r * 8) + c + seat_ids.append(seat_id) +print(f"Highest ID: {max(seat_ids)}") \ No newline at end of file diff --git a/day_5/part_2.py b/day_5/part_2.py new file mode 100644 index 0000000..c934dc9 --- /dev/null +++ b/day_5/part_2.py @@ -0,0 +1,39 @@ +def binary_partition(instructions, area, lesser): + # Base Case: no instructions remaining + if len(instructions) == 0: + if instructions == lesser: + return area[0] + else: + return area[1] + + # Recursive Case: Multiple instructions remaining + mid = ((area[1] - area[0]) // 2) + area[0] + if instructions[0] == lesser: + return binary_partition(instructions[1:], [area[0], mid], lesser) + else: + return binary_partition(instructions[1:], [mid, area[1]], lesser) + + +def row(instructions): + return binary_partition(instructions, [0, 127], "F") + +def col(instructions): + return binary_partition(instructions, [0, 7], "L") + + +with open("input") as f: + data = f.read().split("\n") + +seat_ids = [] + +for boarding_pass in data: + r = row(boarding_pass[:-3]) + c = col(boarding_pass[-3:]) + seat_id = (r * 8) + c + seat_ids.append(seat_id) + +seat_ids.sort() + +for seat in seat_ids: + if seat + 1 not in seat_ids and seat + 2 in seat_ids: + print(f"Missing seat: {seat + 1}") \ No newline at end of file