Watch
1
0
Fork
You've already forked advent-of-code
0
advent-of-code/2020/day15/day15.ex
2021-12-04 21:20:57 -08:00

45 lines
762 B
Elixir

defmodule Day15 do
defp read_input do
Path.expand('input', Path.dirname(__ENV__.file))
|> File.read!()
end
defp test_input do
"""
0,3,6
"""
end
defp parse_numbers(text) do
text
|> String.trim()
|> String.split(",")
|> Enum.map(&String.to_integer/1)
end
defp next([previous | rest]) do
{_, idx} =
rest
|> Enum.with_index()
|> Enum.find({previous, -1}, fn {num, idx} -> num == previous end)
idx + 1
end
defp run(log, limit) do
if Enum.count(log) == limit do
Enum.at(log, 0)
else
run([next(log) | log], limit)
end
end
def part1 do
read_input()
|> parse_numbers()
|> Enum.reverse()
|> run(2020)
end
end
Day15.part1() |> IO.inspect()