Created
December 14, 2015 20:04
-
-
Save AntonFagerberg/0c7972cf299fa24c0bd6 to your computer and use it in GitHub Desktop.
This file contains 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 Day12 do | |
def run do | |
"/Users/anton/Desktop/input" | |
|> File.read! | |
|> Poison.decode! | |
|> process | |
end | |
def process(item) when is_map(item) do | |
if Enum.any?(item, fn {k, v} -> k == "red" || v == "red" end) do | |
0 | |
else | |
item |> Enum.flat_map(fn {key, value} -> [process(key), process(value)] end) |> Enum.sum | |
end | |
end | |
def process(item) when is_list(item), do: item |> Enum.map(&process/1) |> Enum.sum | |
def process(item) when is_integer(item), do: item | |
def process(item) when is_binary(item), do: 0 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment