aoc/2020/lib/days/day5.ex
2023-04-17 20:18:20 +02:00

42 lines
1.1 KiB
Elixir

defmodule AOC.Day5 do
@upper 'BR'
@lower 'FL'
def search_range([], x, x), do: x
def search_range([hd | tl], low, high) when hd in @lower do
search_range(tl, low, div(high + 1 + low, 2) - 1)
end
def search_range([hd | tl], low, high) when hd in @upper do
search_range(tl, div(low + high + 1, 2), high)
end
def search_seat({row_search, col_search}) do
row = search_range(row_search, 0, 127)
column = search_range(col_search, 0, 7)
{row, column}
end
def part1 do
AOC.Util.input_lines(5, 1)
|> Enum.map(&String.to_charlist/1)
|> Enum.map(&Enum.split(&1, 7))
|> Enum.map(&search_seat/1)
|> Enum.map(fn {r, c} -> r * 8 + c end)
|> Enum.max()
end
def find_missing_seat([l, r | _]) when r != l + 1, do: l + 1
def find_missing_seat([_ | tl]), do: find_missing_seat(tl)
def part2 do
AOC.Util.input_lines(5, 1)
|> Enum.map(&String.to_charlist/1)
|> Enum.map(&Enum.split(&1, 7))
|> Enum.map(&search_seat/1)
|> Enum.map(fn {r, c} -> r * 8 + c end)
|> Enum.sort()
|> find_missing_seat()
end
end