Skip to content

Instantly share code, notes, and snippets.

@madlep
Forked from memoriesadrift/aoc-y2018-d1-p2.ex
Last active October 4, 2022 00:04
Show Gist options
  • Save madlep/fc20ea685ec956986b5615088c067266 to your computer and use it in GitHub Desktop.
Save madlep/fc20ea685ec956986b5615088c067266 to your computer and use it in GitHub Desktop.
# example input
["+3", "+3", "+4", "-2", "-4"]
|> Enum.map(&String.to_integer/1)
|> Stream.cycle()
|> Enum.reduce_while({0, MapSet.new([0])}, fn n, {sum, seen} ->
new_sum = sum + n
if MapSet.member?(seen, new_sum) do
{:halt, new_sum}
else
{:cont, {new_sum, MapSet.put(seen, new_sum)}}
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment