2021-05-13 13:05:13 +00:00
|
|
|
list = File.read!("input.txt")
|
2021-05-14 08:53:06 +00:00
|
|
|
|> String.split(~r/\R/)
|
2021-05-13 13:05:13 +00:00
|
|
|
|> Enum.map(&String.to_integer/1)
|
|
|
|
|
|
|
|
ans1 = list
|
|
|
|
|> Enum.sum()
|
|
|
|
IO.puts(ans1)
|
|
|
|
|
2021-05-14 08:53:06 +00:00
|
|
|
reducer = fn i, {current, seen} ->
|
|
|
|
frequency = current + i
|
2021-05-13 13:05:13 +00:00
|
|
|
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)
|