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

--- Day 16: Proboscidea Volcanium ---

Day16/day16.livemd

— Day 16: Proboscidea Volcanium —

Mix.install([
  {:libgraph, "~> 0.16.0"}
])
:ok

Part 1

The sensors have led you to the origin of the distress signal: yet another handheld device, just like the one the Elves gave you. However, you don’t see any Elves around; instead, the device is surrounded by elephants! They must have gotten lost in these tunnels, and one of the elephants apparently figured out how to turn on the distress signal.

The ground rumbles again, much stronger this time. What kind of cave is this, exactly? You scan the cave with your handheld device; it reports mostly igneous rock, some ash, pockets of pressurized gas, magma… this isn’t just a cave, it’s a volcano!

You need to get the elephants out of here, quickly. Your device estimates that you have 30 minutes before the volcano erupts, so you don’t have time to go back out the way you came in.

You scan the cave for other options and discover a network of pipes and pressure-release valves. You aren’t sure how such a system got into a volcano, but you don’t have time to complain; your device produces a report (your puzzle input) of each valve’s flow rate if it were opened (in pressure per minute) and the tunnels you could use to move between the valves.

There’s even a valve in the room you and the elephants are currently standing in labeled AA. You estimate it will take you one minute to open a single valve and one minute to follow any tunnel from one valve to another. What is the most pressure you could release?

For example, suppose you had the following scan output:

example_input = """
Valve AA has flow rate=0; tunnels lead to valves DD, II, BB
Valve BB has flow rate=13; tunnels lead to valves CC, AA
Valve CC has flow rate=2; tunnels lead to valves DD, BB
Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE
Valve EE has flow rate=3; tunnels lead to valves FF, DD
Valve FF has flow rate=0; tunnels lead to valves EE, GG
Valve GG has flow rate=0; tunnels lead to valves FF, HH
Valve HH has flow rate=22; tunnel leads to valve GG
Valve II has flow rate=0; tunnels lead to valves AA, JJ
Valve JJ has flow rate=21; tunnel leads to valve II
"""
"Valve AA has flow rate=0; tunnels lead to valves DD, II, BB\nValve BB has flow rate=13; tunnels lead to valves CC, AA\nValve CC has flow rate=2; tunnels lead to valves DD, BB\nValve DD has flow rate=20; tunnels lead to valves CC, AA, EE\nValve EE has flow rate=3; tunnels lead to valves FF, DD\nValve FF has flow rate=0; tunnels lead to valves EE, GG\nValve GG has flow rate=0; tunnels lead to valves FF, HH\nValve HH has flow rate=22; tunnel leads to valve GG\nValve II has flow rate=0; tunnels lead to valves AA, JJ\nValve JJ has flow rate=21; tunnel leads to valve II\n"

All of the valves begin closed. You start at valve AA, but it must be damaged or jammed or something: its flow rate is 0, so there’s no point in opening it. However, you could spend one minute moving to valve BB and another minute opening it; doing so would release pressure during the remaining 28 minutes at a flow rate of 13, a total eventual pressure release of 28 * 13 = 364. Then, you could spend your third minute moving to valve CC and your fourth minute opening it, providing an additional 26 minutes of eventual pressure release at a flow rate of 2, or 52 total pressure released by valve CC.

Making your way through the tunnels like this, you could probably open many or all of the valves by the time 30 minutes have elapsed. However, you need to release as much pressure as possible, so you’ll need to be methodical. Instead, consider this approach:

== Minute 1 ==
No valves are open.
You move to valve DD.

== Minute 2 ==
No valves are open.
You open valve DD.

== Minute 3 ==
Valve DD is open, releasing 20 pressure.
You move to valve CC.

== Minute 4 ==
Valve DD is open, releasing 20 pressure.
You move to valve BB.

== Minute 5 ==
Valve DD is open, releasing 20 pressure.
You open valve BB.

== Minute 6 ==
Valves BB and DD are open, releasing 33 pressure.
You move to valve AA.

== Minute 7 ==
Valves BB and DD are open, releasing 33 pressure.
You move to valve II.

== Minute 8 ==
Valves BB and DD are open, releasing 33 pressure.
You move to valve JJ.

== Minute 9 ==
Valves BB and DD are open, releasing 33 pressure.
You open valve JJ.

== Minute 10 ==
Valves BB, DD, and JJ are open, releasing 54 pressure.
You move to valve II.

== Minute 11 ==
Valves BB, DD, and JJ are open, releasing 54 pressure.
You move to valve AA.

== Minute 12 ==
Valves BB, DD, and JJ are open, releasing 54 pressure.
You move to valve DD.

== Minute 13 ==
Valves BB, DD, and JJ are open, releasing 54 pressure.
You move to valve EE.

== Minute 14 ==
Valves BB, DD, and JJ are open, releasing 54 pressure.
You move to valve FF.

== Minute 15 ==
Valves BB, DD, and JJ are open, releasing 54 pressure.
You move to valve GG.

== Minute 16 ==
Valves BB, DD, and JJ are open, releasing 54 pressure.
You move to valve HH.

== Minute 17 ==
Valves BB, DD, and JJ are open, releasing 54 pressure.
You open valve HH.

== Minute 18 ==
Valves BB, DD, HH, and JJ are open, releasing 76 pressure.
You move to valve GG.

== Minute 19 ==
Valves BB, DD, HH, and JJ are open, releasing 76 pressure.
You move to valve FF.

== Minute 20 ==
Valves BB, DD, HH, and JJ are open, releasing 76 pressure.
You move to valve EE.

== Minute 21 ==
Valves BB, DD, HH, and JJ are open, releasing 76 pressure.
You open valve EE.

== Minute 22 ==
Valves BB, DD, EE, HH, and JJ are open, releasing 79 pressure.
You move to valve DD.

== Minute 23 ==
Valves BB, DD, EE, HH, and JJ are open, releasing 79 pressure.
You move to valve CC.

== Minute 24 ==
Valves BB, DD, EE, HH, and JJ are open, releasing 79 pressure.
You open valve CC.

== Minute 25 ==
Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure.

== Minute 26 ==
Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure.

== Minute 27 ==
Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure.

== Minute 28 ==
Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure.

== Minute 29 ==
Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure.

== Minute 30 ==
Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure.

This approach lets you release the most pressure possible in 30 minutes with this valve layout, 1651. Work out the steps to release the most pressure in 30 minutes. What is the most pressure you can release?

Part 2

You’re worried that even with an optimal approach, the pressure released won’t be enough. What if you got one of the elephants to help you?

It would take you 4 minutes to teach an elephant how to open the right valves in the right order, leaving you with only 26 minutes to actually execute your plan. Would having two of you working together be better, even if it means having less time? (Assume that you teach the elephant before opening any valves yourself, giving you both the same full 26 minutes.)

In the example above, you could teach the elephant to help you as follows:

== Minute 1 ==
No valves are open.
You move to valve II.
The elephant moves to valve DD.

== Minute 2 ==
No valves are open.
You move to valve JJ.
The elephant opens valve DD.

== Minute 3 ==
Valve DD is open, releasing 20 pressure.
You open valve JJ.
The elephant moves to valve EE.

== Minute 4 ==
Valves DD and JJ are open, releasing 41 pressure.
You move to valve II.
The elephant moves to valve FF.

== Minute 5 ==
Valves DD and JJ are open, releasing 41 pressure.
You move to valve AA.
The elephant moves to valve GG.

== Minute 6 ==
Valves DD and JJ are open, releasing 41 pressure.
You move to valve BB.
The elephant moves to valve HH.

== Minute 7 ==
Valves DD and JJ are open, releasing 41 pressure.
You open valve BB.
The elephant opens valve HH.

== Minute 8 ==
Valves BB, DD, HH, and JJ are open, releasing 76 pressure.
You move to valve CC.
The elephant moves to valve GG.

== Minute 9 ==
Valves BB, DD, HH, and JJ are open, releasing 76 pressure.
You open valve CC.
The elephant moves to valve FF.

== Minute 10 ==
Valves BB, CC, DD, HH, and JJ are open, releasing 78 pressure.
The elephant moves to valve EE.

== Minute 11 ==
Valves BB, CC, DD, HH, and JJ are open, releasing 78 pressure.
The elephant opens valve EE.

(At this point, all valves are open.)

== Minute 12 ==
Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure.

...

== Minute 20 ==
Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure.

...

== Minute 26 ==
Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure.

With the elephant helping, after 26 minutes, the best you could do would release a total of 1707 pressure.

With you and an elephant working together for 26 minutes, what is the most pressure you could release?

Code

import Graph

defmodule Day16 do
  def parse_input(input) do
    for line <- String.split(input, "\n", trim: true),
        reduce: {Graph.new(type: :directed), %{}} do
      acc ->
        {graph, flow_valves} = acc

        flow =
          Regex.run(~r/Valve [A-Z]{2} has flow rate=(\d+);/, line, capture: :all_but_first)
          |> hd()
          |> String.to_integer()

        valves = Regex.scan(~r/[A-Z]{2}/, line) |> List.flatten()
        [valve_source | valves_dest] = valves

        m =
          if flow > 0 do
            Map.put(flow_valves, valve_source, flow)
          else
            flow_valves
          end

        g =
          for v <- valves_dest, reduce: graph do
            acc ->
              Graph.add_vertex(acc, v)
              |> Graph.add_edge(valve_source, v)
          end

        {g, m}
    end
  end

  defp distances(g, fv, start) do
    # is the graph strongly connected? -> order is not important
    # We can't assume strongly connected property so we need to keep the order
    for s <- Map.keys(fv) ++ [start], d <- Map.keys(fv) ++ [start], s != d, reduce: Graph.new() do
      acc ->
        shortest_path = length(Graph.dijkstra(g, s, d))
        shortest_path_rev = length(Graph.dijkstra(g, d, s))

        Graph.add_vertices(acc, [s, d])
        |> Graph.add_edge(s, d, weight: shortest_path - 1)
        |> Graph.add_edge(d, s, weight: shortest_path_rev - 1)
    end
  end

  defp generate_paths(g, fv, v, time, flow, visited, map) do
    for u <- Map.keys(fv), u != v, reduce: Map.put(map, visited, flow) do
      acc ->
        arrival_time = time - Graph.edge(g, v, u).weight - 1

        if arrival_time > 0 and u not in visited do
          f = flow + fv[u] * arrival_time
          generate_paths(g, fv, u, arrival_time, f, visited ++ [u], acc)
        else
          acc
        end
    end
  end

  def part1(input) do
    {graph, flow_valves} = input |> parse_input()
    flow_graph = distances(graph, flow_valves, "AA")
    m = generate_paths(flow_graph, flow_valves, "AA", 30, 0, [], %{})
    {_path, max} = Enum.sort(m, fn {_k1, val1}, {_k2, val2} -> val1 >= val2 end) |> hd()
    max
  end

  def part2(input) do
    {graph, flow_valves} = input |> parse_input()
    flow_graph = distances(graph, flow_valves, "AA")

    m =
      generate_paths(flow_graph, flow_valves, "AA", 26, 0, [], %{})
      |> Enum.sort(fn {_k1, val1}, {_k2, val2} -> val1 >= val2 end)

    for {k1, v1} <- m, {k2, v2} <- m, length(k1 -- k2) == length(k1), reduce: 0 do
      acc ->
        cond do
          v1 + v2 > acc -> v1 + v2
          true -> acc
        end
    end
  end
end
{:module, Day16, <<70, 79, 82, 49, 0, 0, 28, ...>>, {:part2, 1}}

Checking that the example input produces the right result:

Day16.part1(example_input) == 1651
true
input = File.read!("Day16/input.txt")
Day16.part1(input)
2183
Day16.part2(example_input)
1707

Using real input data (saved on a file as it’s too long to show here):

Day16.part2(input)
2911