aoc/2018/01/01.exs
2021-05-13 08:05:13 -05:00

21 lines
412 B
Elixir

list = File.read!("input.txt")
|> String.split("\n")
|> 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)