2018-12-01 22:55:16 +00:00
|
|
|
defmodule Day1 do
|
|
|
|
@initial_state %{found: [], sum: 0}
|
2018-12-01 07:26:00 +00:00
|
|
|
|
2018-12-03 13:49:39 +00:00
|
|
|
def input() do
|
|
|
|
File.stream!("input/day1.in")
|
2018-12-01 22:55:16 +00:00
|
|
|
|> 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
|
|
|
|
|
2018-12-03 13:49:39 +00:00
|
|
|
numlist = Day1.input()
|
2018-12-01 22:55:16 +00:00
|
|
|
IO.puts(Enum.sum(numlist))
|
|
|
|
Day1.find_first_repeat(numlist)
|