Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defmodule Two do
- def calculate(file) do
- file
- |> File.read!()
- |> String.split("\n")
- |> Stream.reject(&(&1 == ""))
- |> Stream.reject(&is_nil/1)
- |> Stream.map(&String.to_integer/1)
- |> Stream.cycle()
- |> Enum.reduce_while({0, MapSet.new()}, fn i, {current_frequency, seen} ->
- new_frequency = current_frequency + i
- if MapSet.member?(seen, new_frequency) do
- {:halt, new_frequency}
- else
- {:cont, {new_frequency, MapSet.put(seen, new_frequency)}}
- end
- end)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement