Skip to content

Instantly share code, notes, and snippets.

@ktec
Created January 2, 2016 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktec/634a667ad0f6b652e856 to your computer and use it in GitHub Desktop.
Save ktec/634a667ad0f6b652e856 to your computer and use it in GitHub Desktop.
defmodule Day1 do
defp _position(str) do
str
|> Stream.unfold(&String.next_codepoint/1)
|> Stream.map(&do_decode/1)
|> Stream.with_index
|> Stream.transform(0, &do_floor/2)
|> Stream.map(&IO.inspect(&1))
|> Enum.to_list
|> List.last
end
defp do_decode("("), do: 1
defp do_decode(")"), do: -1
defp do_decode(_), do: 0
defp do_floor({x,index}, acc) when acc + x != -1 do
floor = acc + x
position = index + 1
IO.puts "position: #{position}, floor: #{floor}"
{[{position, floor}], floor}
end
defp do_floor({x,index}, acc) do
floor = acc + x
position = index + 1
IO.puts "Found the floor -1 at position: #{position}"
{:halt, position}
end
end
@ktec
Copy link
Author

ktec commented Jan 2, 2016

Output:

iex(44)> Day1.position("())")
position: 1, floor: 1
{1, 1}
position: 2, floor: 0
{2, 0}
Found the floor -1 at position: 3
{2, 0}

@ktec
Copy link
Author

ktec commented Jan 2, 2016

The output from the Day1.position("())") call returns {2,0} but really I want to get the position value of 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment