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

--- Day 21: Monkey Math ---

Day21/day21.livemd

— Day 21: Monkey Math —

Mix.install([])
:ok

Part 1

The monkeys are back! You’re worried they’re going to try to steal your stuff again, but it seems like they’re just holding their ground and making various monkey noises at you.

Eventually, one of the elephants realizes you don’t speak monkey and comes over to interpret. As it turns out, they overheard you talking about trying to find the grove; they can show you a shortcut if you answer their riddle.

Each monkey is given a job: either to yell a specific number or to yell the result of a math operation. All of the number-yelling monkeys know their number from the start; however, the math operation monkeys need to wait for two other monkeys to yell a number, and those two other monkeys might also be waiting on other monkeys.

Your job is to work out the number the monkey named root will yell before the monkeys figure it out themselves.

For example:

example_input = """
root: pppw + sjmn
dbpl: 5
cczh: sllz + lgvd
zczc: 2
ptdq: humn - dvpt
dvpt: 3
lfqf: 4
humn: 5
ljgn: 2
sjmn: drzm * dbpl
sllz: 4
pppw: cczh / lfqf
lgvd: ljgn * ptdq
drzm: hmdt - zczc
hmdt: 32
"""
"root: pppw + sjmn\ndbpl: 5\ncczh: sllz + lgvd\nzczc: 2\nptdq: humn - dvpt\ndvpt: 3\nlfqf: 4\nhumn: 5\nljgn: 2\nsjmn: drzm * dbpl\nsllz: 4\npppw: cczh / lfqf\nlgvd: ljgn * ptdq\ndrzm: hmdt - zczc\nhmdt: 32\n"

Each line contains the name of a monkey, a colon, and then the job of that monkey:

  • A lone number means the monkey’s job is simply to yell that number.
  • A job like aaaa + bbbb means the monkey waits for monkeys aaaa and bbbb to yell each of their numbers; the monkey then yells the sum of those two numbers.
  • aaaa - bbbb means the monkey yells aaaa’s number minus bbbb’s number.
  • Job aaaa * bbbb will yell aaaa’s number multiplied by bbbb’s number.
  • Job aaaa / bbbb will yell aaaa’s number divided by bbbb’s number.

So, in the above example, monkey drzm has to wait for monkeys hmdt and zczc to yell their numbers. Fortunately, both hmdt and zczc have jobs that involve simply yelling a single number, so they do this immediately: 32 and 2. Monkey drzm can then yell its number by finding 32 minus 2: 30.

Then, monkey sjmn has one of its numbers (30, from monkey drzm), and already has its other number, 5, from dbpl. This allows it to yell its own number by finding 30 multiplied by 5: 150.

This process continues until root yells a number: 152.

However, your actual situation involves considerably more monkeys. What number will the monkey named root yell?

Part 2

Due to some kind of monkey-elephant-human mistranslation, you seem to have misunderstood a few key details about the riddle.

First, you got the wrong job for the monkey named root; specifically, you got the wrong math operation. The correct operation for monkey root should be =, which means that it still listens for two numbers (from the same two monkeys as before), but now checks that the two numbers match.

Second, you got the wrong monkey for the job starting with humn:. It isn’t a monkey - it’s you. Actually, you got the job wrong, too: you need to figure out what number you need to yell so that root’s equality check passes. (The number that appears after humn: in your input is now irrelevant.)

In the above example, the number you need to yell to pass root’s equality test is 301. (This causes root to get the same number, 150, from both of its monkeys.)

What number do you yell to pass root’s equality test?

Code

defmodule BinaryNode do
  defstruct [:type, :val, :op, :left, :right, :humn_branch]
end

defmodule Day21 do
  defp parse_input(input) do
    for l <- String.split(input, "\n", trim: true), reduce: %{} do
      acc ->
        [monkey, command] = String.split(l, ":", trim: true)

        node =
          cond do
            match = Regex.run(~r/^\s*(\d+)\s*$/, command, capture: :all_but_first) ->
              %BinaryNode{type: :leaf, val: String.to_integer(List.first(match))}

            match =
                Regex.run(~r/\s*(\w+)\s*([*\/+-])\s*(\w+)\s*$/, command, capture: :all_but_first) ->
              %BinaryNode{
                type: :oper,
                op: Enum.at(match, 1),
                left: Enum.at(match, 0),
                right: Enum.at(match, 2)
              }
          end

        Map.put(acc, monkey, node)
    end
  end

  defp fn_op(op) do
    case op do
      "+" -> fn x, y -> x + y end
      "-" -> fn x, y -> x - y end
      "*" -> fn x, y -> x * y end
      "/" -> fn x, y -> div(x, y) end
    end
  end

  defp eval_node(tree, node_name) do
    node = tree[node_name]

    if node.type == :leaf do
      tree
    else
      parsed_tree =
        tree
        |> eval_node(node.left)
        |> eval_node(node.right)

      val = fn_op(node.op).(parsed_tree[node.left].val, parsed_tree[node.right].val)

      # {val, _} = Code.eval_string("l " <> node.op <> " r", [l: parsed_tree[node.left].val, r: parsed_tree[node.right].val])
      Map.update!(parsed_tree, node_name, fn x -> %BinaryNode{x | val: val} end)
    end
  end

  defp search_parent(tree, node_name) do
    Enum.reduce_while(tree, nil, fn {name, node}, acc ->
      if node.left == node_name or node.right == node_name, do: {:halt, name}, else: {:cont, acc}
    end)
  end

  defp find_branch(tree, "root") do
    tree
  end

  defp find_branch(tree, start_node) do
    p = search_parent(tree, start_node)

    branch =
      cond do
        tree[p].left == start_node -> :left
        tree[p].right == start_node -> :right
      end

    t = Map.update!(tree, p, fn x -> %BinaryNode{x | humn_branch: branch} end)
    find_branch(t, p)
  end

  defp inverse(op) do
    case op do
      "+" -> fn_op("-")
      "-" -> fn_op("+")
      "/" -> fn_op("*")
      "*" -> fn_op("/")
    end
  end

  defp navigate(tree, "root") do
    {succ, desired_val} = succ_val(tree, "root")
    navigate(tree, succ, desired_val)
  end

  defp navigate(_tree, "humn", desired_val) do
    desired_val
  end

  defp navigate(tree, start_node, desired_val) do
    {succ, child_val} = succ_val(tree, start_node)
    node = tree[start_node]

    b_val =
      case node.op do
        "-" ->
          if node.humn_branch == :left,
            do: fn_op("+").(desired_val, child_val),
            else: fn_op("-").(child_val, desired_val)

        "/" ->
          if node.humn_branch == :left,
            do: fn_op("*").(desired_val, child_val),
            else: fn_op("/").(child_val, desired_val)

        _ ->
          inverse(node.op).(desired_val, child_val)
      end

    navigate(tree, succ, b_val)
  end

  defp succ_val(tree, node_name) do
    node = tree[node_name]

    case node.humn_branch do
      :left -> {node.left, tree[node.right].val}
      :right -> {node.right, tree[node.left].val}
    end
  end

  def part1(input) do
    (input
     |> parse_input()
     |> eval_node("root")
     |> get_in(["root"])).val
  end

  def part2(input) do
    tree =
      input
      |> parse_input()
      |> eval_node("root")
      |> find_branch("humn")

    navigate(tree, "root")
  end
end
{:module, Day21, <<70, 79, 82, 49, 0, 0, 38, ...>>, {:part2, 1}}

Checking that the example input produces the right result:

Day21.part1(example_input) == 152
true
input = File.read!("Day21/input.txt")
Day21.part1(input)
83056452926300
Day21.part2(example_input) == 301
true
Day21.part2(input)
3469704905529