Skip to content

Instantly share code, notes, and snippets.

@maruks
Created December 13, 2017 00:21
Show Gist options
  • Save maruks/3913438fef74cdff7a056ea5df7565b3 to your computer and use it in GitHub Desktop.
Save maruks/3913438fef74cdff7a056ea5df7565b3 to your computer and use it in GitHub Desktop.
Advent of Code, Day 12
defmodule Plumber do
def input do
"/Users/maris/Projects/Advent_of_code_2017/plumber/test/input" |>
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 do
count([], 0, input())
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment