This commit is contained in:
Shadowfacts 2020-12-05 10:12:14 -05:00
parent 8acd351f84
commit ac51a553e6
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 905 additions and 1 deletions

View File

@ -120,7 +120,7 @@ defmodule Day4 do
iex> Day4.validate_pid("000000001") iex> Day4.validate_pid("000000001")
true true
iex> Day4.validate_pid("0123456789") iex> Day4.validate_pid("0123456789")
true false
""" """
def validate_pid(v) do def validate_pid(v) do
Regex.match?(~r/^\d{9}$/, v) Regex.match?(~r/^\d{9}$/, v)

84
lib/day5/day5.ex Normal file
View File

@ -0,0 +1,84 @@
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

816
lib/day5/input.txt Normal file
View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