Day 12: JSAbacusFramework.io
Mix.install([
{:jason, "~> 1.2"}
])
Section
defmodule P1 do
def sum_recursive(map) when is_map(map) do
map
|> Map.values()
|> Enum.map(&sum_recursive/1)
|> Enum.sum()
end
def sum_recursive(list) when is_list(list) do
list
|> Enum.map(&sum_recursive/1)
|> Enum.sum()
end
def sum_recursive(number) when is_number(number), do: number
def sum_recursive(_), do: 0
end
json_string =
"/Users/eli/Desktop/input.txt"
|> File.read!()
{:ok, result} = Jason.decode(json_string)
P1.sum_recursive(result)
119433
defmodule P2 do
def sum_recursive(map) when is_map(map) do
if Map.values(map) |> Enum.any?(fn v -> v == "red" end) do
0
else
map
|> Map.values()
|> Enum.map(&sum_recursive/1)
|> Enum.sum()
end
end
def sum_recursive(list) when is_list(list) do
list
|> Enum.map(&sum_recursive/1)
|> Enum.sum()
end
def sum_recursive(number) when is_number(number), do: number
def sum_recursive(_), do: 0
end
json_string =
"/Users/eli/Desktop/input.txt"
|> File.read!()
{:ok, result} = Jason.decode(json_string)
P2.sum_recursive(result)
68466