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

Day 8: Haunted Wasteland

2023/08/sunghwa.jun/day_08.livemd

Day 8: Haunted Wasteland

Mix.install([
  {:math, "~> 0.7.0"}
])

Part 1

You’re still riding a camel across Desert Island when you spot a sandstorm quickly approaching. When you turn to warn the Elf, she disappears before your eyes! To be fair, she had just finished warning you about ghosts a few minutes ago.

One of the camel’s pouches is labeled “maps” - sure enough, it’s full of documents (your puzzle input) about how to navigate the desert. At least, you’re pretty sure that’s what they are; one of the documents contains a list of left/right instructions, and the rest of the documents seem to describe some kind of network of labeled nodes.

It seems like you’re meant to use the left/right instructions to navigate the network. Perhaps if you have the camel follow the same instructions, you can escape the haunted wasteland!

After examining the maps for a bit, two nodes stick out: AAA and ZZZ. You feel like AAA is where you are now, and you have to follow the left/right instructions until you reach ZZZ.

This format defines each node of the network individually. For example:

RL

AAA = (BBB, CCC)
BBB = (DDD, EEE)
CCC = (ZZZ, GGG)
DDD = (DDD, DDD)
EEE = (EEE, EEE)
GGG = (GGG, GGG)
ZZZ = (ZZZ, ZZZ)

LLR

AAA = (BBB, BBB) BBB = (AAA, ZZZ) ZZZ = (ZZZ, ZZZ)

Starting at AAA, follow the left/right instructions. How many steps are required to reach ZZZ?

Input

input = "LLR

AAA = (BBB, BBB)
BBB = (AAA, ZZZ)
ZZZ = (ZZZ, ZZZ)"

input = "LR

11A = (11B, XXX)
11B = (XXX, 11Z)
11Z = (11B, XXX)
22A = (22B, XXX)
22B = (22C, 22C)
22C = (22Z, 22Z)
22Z = (22B, 22B)
XXX = (XXX, XXX)"

input = File.read!(__DIR__ <> "/day_08.in")

Parse

[instructions, _ | nodes] = String.split(input, "\n")

nodes =
  nodes
  |> Enum.map(fn node_string ->
    [key, _, left, right] = String.split(node_string)
    left = String.slice(left, 1..3)
    right = String.slice(right, 0..2)
    {key, %{left: left, right: right}}
  end)
  |> Map.new()

Solution

instruction_stream = instructions |> String.graphemes() |> Stream.cycle()

Enum.reduce_while(instruction_stream, {"", 0}, fn instruction, {node, count} ->
  action = if instruction == "L", do: :left, else: :right

  case node do
    "" ->
      {:cont, {nodes["AAA"][action], count + 1}}

    "ZZZ" ->
      {:halt, {"ZZZ", count}}

    _ ->
      {:cont, {nodes[node][action], count + 1}}
  end
end)

Part 2

— Part Two — The sandstorm is upon you and you aren’t any closer to escaping the wasteland. You had the camel follow the instructions, but you’ve barely left your starting position. It’s going to take significantly more steps to escape!

What if the map isn’t for people - what if the map is for ghosts? Are ghosts even bound by the laws of spacetime? Only one way to find out.

After examining the maps a bit longer, your attention is drawn to a curious fact: the number of nodes with names ending in A is equal to the number ending in Z! If you were a ghost, you’d probably just start at every node that ends with A and follow all of the paths at the same time until they all simultaneously end up at nodes that end with Z.

For example:

LR

11A = (11B, XXX)
11B = (XXX, 11Z)
11Z = (11B, XXX)
22A = (22B, XXX)
22B = (22C, 22C)
22C = (22Z, 22Z)
22Z = (22B, 22B)
XXX = (XXX, XXX)

Here, there are two starting nodes, 11A and 22A (because they both end with A). As you follow each left/right instruction, use that instruction to simultaneously navigate away from both nodes you’re currently on. Repeat this process until all of the nodes you’re currently on end with Z. (If only some of the nodes you’re on end with Z, they act like any other node and you continue as normal.) In this example, you would proceed as follows:

Step 0: You are at 11A and 22A.
Step 1: You choose all of the left paths, leading you to 11B and 22B.
Step 2: You choose all of the right paths, leading you to 11Z and 22C.
Step 3: You choose all of the left paths, leading you to 11B and 22Z.
Step 4: You choose all of the right paths, leading you to 11Z and 22B.
Step 5: You choose all of the left paths, leading you to 11B and 22C.
Step 6: You choose all of the right paths, leading you to 11Z and 22Z.
So, in this example, you end up entirely on nodes that end in Z after 6 steps.

Simultaneously start on every node that ends with A. How many steps does it take before you’re only on nodes that end with Z?

start_nodes = Enum.filter(nodes, fn {key, _value} -> String.at(key, 2) == "A" end)

Enum.map(start_nodes, fn {start_node, _} ->
  {_, count} =
    Enum.reduce_while(instruction_stream, {start_node, 0}, fn instruction, {node, count} ->
      action = if instruction == "L", do: :left, else: :right

      if String.at(node, 2) == "Z" do
        {:halt, {"Z", count}}
      else
        {:cont, {nodes[node][action], count + 1}}
      end
    end)

  count
end)
|> Enum.reduce(1, fn number, acc ->
  Math.lcm(number, acc)
end)