Skip to content

Instantly share code, notes, and snippets.

@maruks
Created December 18, 2017 13:26
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 maruks/af0a24d39de18480c54e966f962d1c77 to your computer and use it in GitHub Desktop.
Save maruks/af0a24d39de18480c54e966f962d1c77 to your computer and use it in GitHub Desktop.
refactored
defmodule Plumber do
def input(file) do
file
|> File.stream!()
|> Enum.map(&(Regex.split(~r/\D+/, String.trim(&1))))
|> Enum.reduce(%{}, fn([first | rest],acc) -> Map.put(acc,first,rest) end )
end
def count([], acc, input) do
case Map.keys(input) do
[first | _ ] -> count(Map.get(input,first,[]), acc + 1, Map.delete(input, first))
[] -> acc
end
end
def count([first | rest], acc, input) do
queue = Map.get(input, first, []) |> Enum.filter(&(Map.has_key?(input,&1)))
count(queue ++ rest, acc, Map.delete(input, first))
end
def howMany(file) do
count([], 0, input(file))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment