Day 15 Part 1
This commit is contained in:
parent
c8fa4449b2
commit
eb17b2431a
1 changed files with 45 additions and 0 deletions
45
2020/day15/day15.ex
Normal file
45
2020/day15/day15.ex
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
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()
|
||||||
Loading…
Reference in a new issue