Created
December 18, 2017 13:26
-
-
Save maruks/af0a24d39de18480c54e966f962d1c77 to your computer and use it in GitHub Desktop.
refactored
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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