2020 - Day 5 (Binary Boarding)

View puzzle description on AdventOfCode.com
Part 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python3

#Advent of Code
#2020 Day 5, Part 1
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

def solve(puzzle_input):
    return max(int(boarding_pass.replace("F", "0").replace("B", "1").replace("L", "0").replace("R", "1"), 2) for boarding_pass in puzzle_input)

def main():
    puzzle_input = util.read.as_lines()

    highest_seat_id = solve(puzzle_input)

    print("The highest seat ID is " + str(highest_seat_id) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["FBFBBFFRLR"]), 357)

    def test_ex2(self):
        return self.assertEqual(solve(["BFFFBBFRRR"]), 567)

    def test_ex3(self):
        return self.assertEqual(solve(["FFFBBBFRRR"]), 119)

    def test_ex4(self):
        return self.assertEqual(solve(["BBFFBBFRLL"]), 820)

if __name__ == "__main__":
    run(main)
Part 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python3

#Advent of Code
#2020 Day 5, Part 2
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

def solve(puzzle_input):
    seats = sorted(int(boarding_pass.replace("F", "0").replace("B", "1").replace("L", "0").replace("R", "1"), 2) for boarding_pass in puzzle_input)
    for expected, actual in zip(range(seats[0], seats[-1]+1), seats):
        if expected != actual:
            return expected

def main():
    puzzle_input = util.read.as_lines()

    seat_id = solve(puzzle_input)

    print("The seat ID is " + str(seat_id) + ".")

class AOC_Tests(unittest.TestCase):
    @unittest.skip("No test cases")
    def test_ex1(self):
        pass

if __name__ == "__main__":
    run(main)
Return to solution index