add naive solution for part 2

This commit is contained in:
Ben Harris 2018-12-01 17:55:16 -05:00
parent 3348e58afe
commit b3c1536aa9
Signed by: ben
GPG Key ID: 4E0AF802FFF7960C
1 changed files with 31 additions and 6 deletions

View File

@ -1,7 +1,32 @@
File.stream!("day1.in")
|> Stream.map(fn x ->
x |> String.trim() |> String.replace_leading("+", "") |> String.to_integer()
end)
|> Enum.sum()
|> IO.puts()
defmodule Day1 do
@initial_state %{found: [], sum: 0}
def get_numlist do
File.stream!("day1.in")
|> Stream.map(&String.to_integer(String.trim(&1)))
end
def find_first_repeat(numlist, acc \\ @initial_state) do
res = find_repeat(numlist, acc)
find_first_repeat(numlist, res)
end
def find_repeat(numlist, acc_init) do
numlist
|> Enum.reduce(acc_init, fn x, acc ->
newval = x + acc.sum
if newval in acc.found do
IO.puts("repeated frequency found: #{newval}")
exit(0)
end
%{found: [newval | acc.found], sum: newval}
end)
end
end
numlist = Day1.get_numlist()
IO.puts(Enum.sum(numlist))
Day1.find_first_repeat(numlist)