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

Day 13

day13.livemd

Day 13

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

IEx.Helpers.c("/Users/johnb/dev/2022adventOfCode/advent_of_code.ex")
alias AdventOfCode, as: AOC
alias Kino.Input

# Note: when making the next template, something like this works well:
#   `cat day04.livemd | sed 's/03/04/' > day04.livemd`
#

Installation and Data

input_p1example = Kino.Input.textarea("Example Data")
input_p1puzzleInput = Kino.Input.textarea("Puzzle Input")
input_source_select =
  Kino.Input.select("Source", [{:example, "example"}, {:puzzle_input, "puzzle input"}])
p1data = fn ->
  (Kino.Input.read(input_source_select) == :example &&
     Kino.Input.read(input_p1example)) ||
    Kino.Input.read(input_p1puzzleInput)
end

Part 1

###

Day 13: Distress Signal

You climb the hill and again try contacting the Elves. However, you instead receive a signal you weren’t expecting: a distress signal.

Your handheld device must still not be working properly; the packets from the distress signal got decoded out of order. You’ll need to re-order the list of received packets (your puzzle input) to decode the message.

Your list consists of pairs of packets; pairs are separated by a blank line. You need to identify how many pairs of packets are in the right order.

For example:

[1,1,3,1,1]
[1,1,5,1,1]

[[1],[2,3,4]]
[[1],4]

[9]
[[8,7,6]]

[[4,4],4,4]
[[4,4],4,4,4]

[7,7,7,7]
[7,7,7]

[]
[3]

[[[]]]
[[]]

[1,[2,[3,[4,[5,6,7]]]],8,9]
[1,[2,[3,[4,[5,6,0]]]],8,9]

Packet data consists of lists and integers. Each list starts with [, ends with ], and contains zero or more comma-separated values (either integers or other lists). Each packet is always a list and appears on its own line.

When comparing two values, the first value is called left and the second value is called right. Then:

  • If both values are integers, the lower integer should come first. If the left integer is lower than the right integer, the inputs are in the right order. If the left integer is higher than the right integer, the inputs are not in the right order. Otherwise, the inputs are the same integer; continue checking the next part of the input.
  • If both values are lists, compare the first value of each list, then the second value, and so on. If the left list runs out of items first, the inputs are in the right order. If the right list runs out of items first, the inputs are not in the right order. If the lists are the same length and no comparison makes a decision about the order, continue checking the next part of the input.
  • If exactly one value is an integer, convert the integer to a list which contains that integer as its only value, then retry the comparison. For example, if comparing [0,0,0] and 2, convert the right value to [2] (a list containing 2); the result is then found by instead comparing [0,0,0] and [2].

Using these rules, you can determine which of the pairs in the example are in the right order:

== Pair 1 ==

  • Compare [1,1,3,1,1] vs [1,1,5,1,1]
    • Compare 1 vs 1
    • Compare 1 vs 1
    • Compare 3 vs 5
      • Left side is smaller, so inputs are in the right order

== Pair 2 ==

  • Compare [[1],[2,3,4]] vs [[1],4]
    • Compare [1] vs [1]
      • Compare 1 vs 1
    • Compare [2,3,4] vs 4
      • Mixed types; convert right to [4] and retry comparison
      • Compare [2,3,4] vs [4]
        • Compare 2 vs 4
          • Left side is smaller, so inputs are in the right order

== Pair 3 ==

  • Compare [9] vs [[8,7,6]]
    • Compare 9 vs [8,7,6]
      • Mixed types; convert left to [9] and retry comparison
      • Compare [9] vs [8,7,6]
        • Compare 9 vs 8
          • Right side is smaller, so inputs are not in the right order

== Pair 4 ==

  • Compare [[4,4],4,4] vs [[4,4],4,4,4]
    • Compare [4,4] vs [4,4]
      • Compare 4 vs 4
      • Compare 4 vs 4
    • Compare 4 vs 4
    • Compare 4 vs 4
    • Left side ran out of items, so inputs are in the right order

== Pair 5 ==

  • Compare [7,7,7,7] vs [7,7,7]
    • Compare 7 vs 7
    • Compare 7 vs 7
    • Compare 7 vs 7
    • Right side ran out of items, so inputs are not in the right order

== Pair 6 ==

  • Compare [] vs [3]
    • Left side ran out of items, so inputs are in the right order

== Pair 7 ==

  • Compare [[[]]] vs [[]]
    • Compare [[]] vs []
      • Right side ran out of items, so inputs are not in the right order

== Pair 8 ==

  • Compare [1,[2,[3,[4,[5,6,7]]]],8,9] vs [1,[2,[3,[4,[5,6,0]]]],8,9]
    • Compare 1 vs 1
    • Compare [2,[3,[4,[5,6,7]]]] vs [2,[3,[4,[5,6,0]]]]
      • Compare 2 vs 2
      • Compare [3,[4,[5,6,7]]] vs [3,[4,[5,6,0]]]
        • Compare 3 vs 3
        • Compare [4,[5,6,7]] vs [4,[5,6,0]]
          • Compare 4 vs 4
          • Compare [5,6,7] vs [5,6,0]
            • Compare 5 vs 5
            • Compare 6 vs 6
            • Compare 7 vs 0
              • Right side is smaller, so inputs are not in the right order What are the indices of the pairs that are already in the right order? (The first pair has index 1, the second pair has index 2, and so on.) In the above example, the pairs in the right order are 1, 2, 4, and 6; the sum of these indices is 13.

Determine which pairs of packets are already in the right order. What is the sum of the indices of those pairs?

###

defmodule Day13 do
  def solve(text) do
    text
    |> AOC.as_doublespaced_paragraphs()
    |> Enum.with_index()
    |> Enum.map(fn {left_and_right, index0} ->
      one_based_index = index0 + 1
      [left_string, right_string] = String.split(left_and_right, "\n")

      # Magic (seriously dangerous magic) happens here.
      # Part 2 will likely deprive me of this crutch.
      {left_list, []} = Code.eval_string(left_string)
      {right_list, []} = Code.eval_string(right_string)

      %{
        index: one_based_index,
        order: compare(left_list, right_list)
      }

      # |> IO.inspect(label: "Pair #{one_based_index} #{inspect(left_list)} vs #{inspect(right_list)}")
    end)
    |> Enum.filter(fn %{order: order} -> order == :right_order end)
    |> Enum.map(fn %{index: index} -> index end)
    |> Enum.sum()
  end

  def solve2(_text) do
  end

  def compare([], []), do: :equal

  def compare([left_int | left_rest], [right_int | right_rest])
      when is_integer(left_int) and is_integer(right_int) do
    cond do
      left_int < right_int -> :right_order
      right_int < left_int -> :wrong_order
      true -> compare(left_rest, right_rest)
    end
  end

  def compare([left_int | left_rest], [right_list | right_rest])
      when is_integer(left_int) do
    compare([[left_int] | left_rest], [right_list | right_rest])
  end

  def compare([left_list | left_rest], [right_int | right_rest])
      when is_integer(right_int) do
    compare([left_list | left_rest], [[right_int] | right_rest])
  end

  def compare([], _right_list), do: :right_order
  def compare(_left_list, []), do: :wrong_order

  def compare([left_list | left_rest], [right_list | right_rest]) do
    case compare(left_list, right_list) do
      :right_order -> :right_order
      :wrong_order -> :wrong_order
      _ -> compare(left_rest, right_rest)
    end
  end
end

p1data.()
|> Day13.solve()
|> IO.inspect(label: "\n*** Part 1 solution (example: 13)")

# 6076 (nope - wrong user! - because chrome garbled my input)
# 5808 (with input from Safari)

# p1data.()
# |> Day13.solve2()
# |> IO.inspect(label: "\n*** Part 2 solution (example: )")

#

Part Two