Day 5 - Advent of Code 2020
# Mix.install([:kino, :benchee])
Links
Prompt
--- Day 5: Binary Boarding ---
You board your plane only to discover a new problem: you dropped your boarding pass! You aren't sure which seat is yours, and all of the flight attendants are busy with the flood of people that suddenly made it through passport control.
You write a quick program to use your phone's camera to scan all of the nearby boarding passes (your puzzle input); perhaps you can find your seat through process of elimination.
Instead of zones or groups, this airline uses binary space partitioning to seat people. A seat might be specified like FBFBBFFRLR, where F means "front", B means "back", L means "left", and R means "right".
The first 7 characters will either be F or B; these specify exactly one of the 128 rows on the plane (numbered 0 through 127). Each letter tells you which half of a region the given seat is in. Start with the whole list of rows; the first letter indicates whether the seat is in the front (0 through 63) or the back (64 through 127). The next letter indicates which half of that region the seat is in, and so on until you're left with exactly one row.
For example, consider just the first seven characters of FBFBBFFRLR:
Start by considering the whole range, rows 0 through 127.
Fmeans to take the lower half, keeping rows 0 through 63.Bmeans to take the upper half, keeping rows 32 through 63.Fmeans to take the lower half, keeping rows 32 through 47.Bmeans to take the upper half, keeping rows 40 through 47.Bkeeps rows44through47.Fkeeps rows44through45.- The final F keeps the lower of the two, row 44.
The last three characters will be either L or R; these specify exactly one of the 8 columns of seats on the plane (numbered 0 through 7). The same process as above proceeds again, this time with only three steps. L means to keep the lower half, while R means to keep the upper half.
For example, consider just the last 3 characters of FBFBBFFRLR:
- Start by considering the whole range, columns
0through7. Rmeans to take the upper half, keeping columns4through7.Lmeans to take the lower half, keeping columns4through5.- The final
Rkeeps the upper of the two, column5.
So, decoding FBFBBFFRLR reveals that it is the seat at row 44, column 5.
Every seat also has a unique seat ID: multiply the row by 8, then add the column. In this example, the seat has ID 44 * 8 + 5 = 357.
Here are some other boarding passes:
BFFFBBFRRR: row70, column7, seat ID567.FFFBBBFRRR: row14, column7, seat ID119.BBFFBBFRLL: row102, column4, seat ID820.
As a sanity check, look through your list of boarding passes. What is the highest seat ID on a boarding pass?
To begin, get your puzzle input.
--- Part Two ---
Ding! The "fasten seat belt" signs have turned on. Time to find your seat.
It's a completely full flight, so your seat should be the only missing boarding pass in your list. However, there's a catch: some of the seats at the very front and back of the plane don't exist on this aircraft, so they'll be missing from your list as well.
Your seat wasn't at the very front or back, though; the seats with IDs +1 and -1 from yours will be in your list.
What is the ID of your seat?
Although it hasn't changed, you can still get your puzzle input.
Input
input = Kino.Input.textarea("Please paste your input file:")
input = input |> Kino.Input.read()
input = AdventOfCode.Input.get!("5", "2020")
"BFFFBBFRLR\nFBFFFBBLRL\nBFFFFFBRRR\nFBBBFFFLRR\nFFBFBFBRLL\nFBBFFBBLRR\nFBBFFFFRLL\nBFFFBBFRLL\nBBFBFBFRLL\nBFFFFFBRLL\nFFBFFFBRRR\nBBFFBFFRLR\nBBFFBBFLLL\nFBFBFBFLLL\nBBFFFFFLLR\nFFBBBFFRLL\nFFBFFBFRRR\nBFFFBBFLRL\nBFBBBBFLRR\nBFBFFBFLLL\nFFBFFBBLRL\nFFBFBFFRLR\nFFBFBBBLLR\nFFFBBFBLLR\nFFFBBBFLRL\nFBFBBBFLLL\nFBBFFFBLLR\nBFFFBBBRLL\nFFBBFBBRRL\nFBFFBBBLLR\nFBFBBFFRRL\nFBBBBFFLLL\nFFBFBFFLLR\nBFFFFBFLLR\nFBBBFBFLRR\nFBFFFFFRRR\nFFBFBBFRRL\nBFFFBBBLLR\nFBBFFBFRLL\nBFFFFBBRLL\nFBBBFFFRRL\nBBFBFFFLLR\nFBBFBBFRLR\nFFFBFFBRRR\nBFBBFBBLRL\nFBBBFFBLRR\nBBFFBBFLRR\nFFBBFBBLLL\nBFFBBBBRRL\nFBBFBFBRLL\nBFFFFFFRRL\nBBFBFBFLLL\nFBFFBFFLLR\nFBFBFBFRLR\nBFBFBBBRLR\nBBFFBBFLLR\nBFFBFBFRRR\nBFBBFBFLLL\nFBFBBFBRLR\nFBBFBFBLRL\nFBFFBFFLRR\nBFFFFFFRRR\nBFFFFBFRLL\nFBFBFFFLLR\nBFFFFBFLRL\nBBFFFFFRLL\nFBFBFFFLRR\nBFFBFB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BRLL\nBBFBFBBLLL\nFFBBBFBLRR\nFBBBFFFRRR\nBFFBBBBLLL\nFBFBFBFRRR\nBFFFFFBLLL\nBFBFBFBLLR\nBBFFFFBRRL\nFBBBFBFLLL\nFFBFFFFLLL\nFBFBFFBLLL\nFBBBBBBRRR\nBFBFFBFLRR\nFFBFBFBLLL\nFFBFFBFRRL\nBFBFBBFRRL\nFFBBFBBLLR\nFBBBBBFLLR\nBFFBFFFRLL\nBBFFFBBLLL\nFBFFFFFLLR\nFFBBBFFRRR\nFBFFFBFRRR\nFFFBBBBLRL\nFBFBFBBRRL\nFBBFBFBLLL\nBFBFFFFLRR\nBBFFBFBLRL\nFFFBFBFRLR\nBFFBBFFLRL\nFFFBBFFRRL\nBFBFBFBRRL\nFFBBBFFLLL\nBBFFFBFLRR\nFFFBBBBLLR\nFFBBBFFRLR\nFFBFBBFLRL\nFFBBBFBLRL\nBFBFFFFRRL\nFBFBBFFLRR\nFBBBBBBLRR\nFBFBBBBRRR\nFFBFBFFLRR\nFBBFBFFRLR\nBFFBBBBRRR\nBFFFBFBLLL\nBFFBBFFLLL\nBFFFFBBLRL\nFFBB" <> ...
Solution
defmodule Day05 do
defdelegate parse(input), to: __MODULE__.Input
@front_chars ~w(F L)
@back_chars ~w(B R)
def part1(input) do
input
|> parse()
|> calc_seat_ids()
|> Enum.max()
end
def part2(input) do
input
|> parse()
|> calc_seat_ids()
|> Enum.sort()
|> find_my_seat()
end
defp calc_seat_ids(input) do
input
|> Enum.map(fn {row, col} ->
{
binary_partition(row, 0..127),
binary_partition(col, 0..7)
}
end)
|> Enum.map(fn {row, col} ->
row * 8 + col
end)
end
defp binary_partition(chars, range)
defp binary_partition([front], f.._b) when front in @front_chars, do: f
defp binary_partition([back], _f..b) when back in @back_chars, do: b
defp binary_partition([f_or_b | rest], range) do
new_size = div(Range.size(range), 2)
new_range = range |> Range.split(new_size) |> front_or_back(f_or_b)
binary_partition(rest, new_range)
end
defp front_or_back({f, _b}, front) when front in @front_chars, do: f
defp front_or_back({_f, b}, back) when back in @back_chars, do: b
defp find_my_seat([a | [b | _]]) when a + 2 == b, do: a + 1
defp find_my_seat([_ | rest]), do: find_my_seat(rest)
defmodule Input do
def parse(input) when is_binary(input) do
input
|> String.splitter("\n", trim: true)
|> parse()
end
def parse(input) do
Stream.map(input, &parse_line/1)
end
def parse_line(line) do
line
|> String.split("", trim: true)
|> Enum.split(7)
end
end
end
{:module, Day05, <<70, 79, 82, 49, 0, 0, 14, ...>>,
{:module, Day05.Input, <<70, 79, 82, ...>>, {:parse_line, 1}}}
What is the highest seat ID on a boarding pass?
Your puzzle answer was 864.
Day05.part1(input)
864
What is the ID of your seat?
Your puzzle answer was 739.
Day05.part2(input)
739
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should return to your Advent calendar and try another puzzle.
If you still want to see it, you can get your puzzle input.