aoc/2018/01/01.exs
2021-05-14 03:53:06 -05:00

22 lines
415 B
Elixir

list = File.read!("input.txt")
|> String.split(~r/\R/)
|> Enum.map(&String.to_integer/1)
ans1 = list
|> Enum.sum()
IO.puts(ans1)
reducer = fn i, {current, seen} ->
frequency = current + i
if MapSet.member?(seen, frequency) do
{:halt, frequency}
else
{:cont, {frequency, MapSet.put(seen, frequency)}}
end
end
ans2 = list
|> Stream.cycle()
|> Enum.reduce_while({0, MapSet.new([0])}, reducer)
IO.puts(ans2)