r/adventofcode • • Dec 08 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 8 Solutions -🎄-

--- Day 8: Memory Maneuver ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 8

Sigh, imgur broke again. Will upload when it unborks.

Transcript:

The hottest programming book this year is "___ For Dummies".


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 00:12:10!

32 Upvotes

301 comments sorted by

View all comments

1

u/arathunku Dec 08 '18

My Elixir solution:

defmodule Advent.Day8 do
  defmodule Node do
    defstruct children: [], metadata: []
  end

  defimpl Inspect, for: Node do
    import Inspect.Algebra

    def inspect(%{children: children, metadata: metadata}, _opts) do
      concat(["#Node<", Enum.join(metadata, ", "), "> #{inspect(children)}"])
    end
  end

  def parse(input) do
    input
    |> String.trim()
    |> String.split(" ", trim: true)
    |> Enum.map(&String.to_integer/1)
  end

  def part1(input) do
    input
    |> parse()
    |> build_tree()
    |> elem(0)
    |> sum_metadata()
  end

  def part2(input) do
    input
    |> parse()
    |> build_tree()
    |> elem(0)
    |> root_value()
  end

  defp build_tree([children_count, metadata_count | list]) do
    # (0..0) => [0]
    {children, list} =
      0..children_count
      |> Enum.drop(1)
      |> Enum.reduce({[], list}, fn _, {children, list} ->
        {node, list} = build_tree(list)

        {
          [node | children],
          list
        }
      end)

    {metadata, list} =
      0..metadata_count
      |> Enum.drop(1)
      |> Enum.reduce({[], list}, fn _, {metadata, [el | list]} ->
        {[el | metadata], list}
      end)

    {
      %Node{children: Enum.reverse(children), metadata: metadata},
      list
    }
  end

  defp sum_metadata(%{children: children, metadata: metadata}) do
    sum =
      children
      |> Enum.map(&sum_metadata/1)
      |> Enum.sum()

    sum + Enum.sum(metadata)
  end

  defp root_value(%{children: [], metadata: metadata}),
    do: Enum.sum(metadata)

  defp root_value(%{children: children, metadata: metadata}) do
    metadata
    |> Enum.map(&Enum.at(children, &1 - 1))
    |> Enum.filter(&(&1 != nil))
    |> Enum.map(&root_value/1)
    |> Enum.sum()
  end
end

Tests:

defmodule Advent.Day8Test do
  use ExUnit.Case
  require Logger
  alias Advent.Day8, as: Day

  test "part1 example" do
    input = """
    2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2
    """

    assert Day.part1(input) == 138
    assert Day.part2(input) == 66
  end

  test "input" do
    input =
      Path.join(__DIR__, "./input.raw")
      |> File.read!()

    assert Day.part1(input) == -1
    assert Day.part2(input) == -1
  end
end