Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Advent of Code 2015 Day 12 Part 2

2015_day12_part2.livemd

Advent of Code 2015 Day 12 Part 2

Mix.install([
  {:kino_aoc, "~> 0.1"}
])

Get Inputs

{:ok, puzzle_input} =
  KinoAOC.download_puzzle("2015", "12", System.fetch_env!("LB_SESSION"))

My answer

defmodule SumIntegers do
  def sum_integers(data) when is_integer(data), do: data

  def sum_integers(data) when is_map(data) do
    values = Map.values(data)

    if Enum.find(values, fn value -> value == "red" end) do
      0
    else
      values
      |> Enum.map(&sum_integers/1)
      |> Enum.sum()
    end
  end

  def sum_integers(data) when is_list(data) do
    data
    |> Enum.map(&sum_integers/1)
    |> Enum.sum()
  end

  def sum_integers(_data), do: 0
end
puzzle_input
|> String.split("\n")
|> Enum.map(fn row -> Jason.decode!(row) end)
|> SumIntegers.sum_integers()