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

Day 16

day16.livemd

Day 16

Mix.install([
  {:kino, "~> 0.8.0"}
])

Section

input = Kino.Input.textarea("Puzzle input")
regex =
  ~r/Valve (?[A-Z]{2}) has flow rate=(?\d+); tunnels? leads? to valves? (?.+)/

{pressures, neighbors} =
  input
  |> Kino.Input.read()
  |> String.split("\n", trim: true)
  |> Enum.reduce({%{}, %{}}, fn line, {pressures, neighbors} ->
    %{
      "name" => name,
      "rate" => pressure_rate,
      "linked_valve" => linked_valve
    } = Regex.named_captures(regex, line)

    pressures = Map.put(pressures, name, String.to_integer(pressure_rate))

    neighbors = Map.put(neighbors, name, String.split(linked_valve, ", ", trim: true))

    {pressures, neighbors}
  end)