85 lines
1.5 KiB
Elixir
85 lines
1.5 KiB
Elixir
|
defmodule Day5 do
|
||
|
def part1 do
|
||
|
input_seat_ids()
|
||
|
|> Enum.max()
|
||
|
end
|
||
|
|
||
|
def part2 do
|
||
|
all_ids = input_seat_ids()
|
||
|
|
||
|
has_id = fn id ->
|
||
|
Enum.any?(all_ids, &(&1 == id))
|
||
|
end
|
||
|
|
||
|
max_id = seat_id({127, 7})
|
||
|
|
||
|
0..max_id
|
||
|
|> Enum.find(fn id ->
|
||
|
!has_id.(id) && has_id.(id - 1) && has_id.(id + 1)
|
||
|
end)
|
||
|
end
|
||
|
|
||
|
def input_seat_ids do
|
||
|
File.read!("lib/day5/input.txt")
|
||
|
|> String.trim()
|
||
|
|> String.split("\n")
|
||
|
|> Enum.map(fn pass ->
|
||
|
pass
|
||
|
|> find_seat()
|
||
|
|> seat_id()
|
||
|
end)
|
||
|
end
|
||
|
|
||
|
@doc """
|
||
|
iex> Day5.find_seat("FBFBBFFRLR")
|
||
|
{44, 5}
|
||
|
"""
|
||
|
def find_seat(str) do
|
||
|
{row, rest} = find_row(str, 0, 127)
|
||
|
col = find_col(rest, 0, 7)
|
||
|
{row, col}
|
||
|
end
|
||
|
|
||
|
def find_row(str, min, max) when min == max do
|
||
|
{min, str}
|
||
|
end
|
||
|
|
||
|
def find_row("F" <> rest, min, max) do
|
||
|
bound = floor((min + max) / 2)
|
||
|
find_row(rest, min, bound)
|
||
|
end
|
||
|
|
||
|
def find_row("B" <> rest, min, max) do
|
||
|
bound = ceil((min + max) / 2)
|
||
|
find_row(rest, bound, max)
|
||
|
end
|
||
|
|
||
|
def find_col(_, min, max) when min == max do
|
||
|
min
|
||
|
end
|
||
|
|
||
|
def find_col("L" <> rest, min, max) do
|
||
|
bound = floor((min + max) / 2)
|
||
|
find_col(rest, min, bound)
|
||
|
end
|
||
|
|
||
|
def find_col("R" <> rest, min, max) do
|
||
|
bound = ceil((min + max) / 2)
|
||
|
find_col(rest, bound, max)
|
||
|
end
|
||
|
|
||
|
@doc """
|
||
|
iex> Day5.seat_id({44, 5})
|
||
|
357
|
||
|
iex> Day5.seat_id({70, 7})
|
||
|
567
|
||
|
iex> Day5.seat_id({14, 7})
|
||
|
119
|
||
|
iex> Day5.seat_id({102, 4})
|
||
|
820
|
||
|
"""
|
||
|
def seat_id({row, col}) do
|
||
|
row * 8 + col
|
||
|
end
|
||
|
end
|