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

Day 06

day06.livemd

Day 06

Setup

https://adventofcode.com/2022/day/06

defmodule Load do
  def input do
    File.read!(Path.join(Path.absname(__DIR__), "input/06.txt"))
  end
end
realInput = Load.input()
testInputs = [
  "asdasdasdasdasd",
  "mjqjpqmgbljsphdztnvjfqwrcgsmlb",
  "bvwbjplbgvbhsrlpgdmjqwftvncz",
  "nppdvjthqldpwncqszvftbrmjlhg",
  "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg",
  "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw"
]

Part 1

To fix the communication system, you need to add a subroutine to the device that detects a start-of-packet marker in the datastream. In the protocol being used by the Elves, the start of a packet is indicated by a sequence of four characters that are all different.

The device will send your subroutine a datastream buffer (your puzzle input); your subroutine needs to identify the first position where the four most recently received characters were all different. Specifically, it needs to report the number of characters from the beginning of the buffer to the end of the first such four-character marker.

For example, suppose you receive the following datastream buffer:

mjqjpqmgbljsphdztnvjfqwrcgsmlb

After the first three characters (mjq) have been received, there haven’t been enough characters received yet to find the marker. The first time a marker could occur is after the fourth character is received, making the most recent four characters mjqj. Because j is repeated, this isn’t a marker.

The first time a marker appears is after the seventh character arrives. Once it does, the last four characters received are jpqm, which are all different. In this case, your subroutine should report the value 7, because the first start-of-packet marker is complete after 7 characters have been processed.

Here are a few more examples:

  • bvwbjplbgvbhsrlpgdmjqwftvncz: first marker after character 5
  • nppdvjthqldpwncqszvftbrmjlhg: first marker after character 6
  • nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg: first marker after character 10
  • zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw: first marker after character 11
defmodule Part1 do
  def solve_recursive(input, marker_length, _) when length(input) <= marker_length do
    nil
  end

  def solve_recursive(input, marker_length, chars_processed) do
    set =
      input
      |> Enum.take(marker_length)
      |> MapSet.new()

    if Enum.count(set) == marker_length do
      chars_processed + marker_length
    else
      solve_recursive(Enum.drop(input, 1), marker_length, chars_processed + 1)
    end
  end

  def solve(input) do
    input
    |> String.to_charlist()
    |> solve_recursive(4, 0)
  end
end

testInputs
|> Enum.map(&amp;Part1.solve(&amp;1))
Part1.solve(realInput)

Part 2

Your device’s communication system is correctly detecting packets, but still isn’t working. It looks like it also needs to look for messages.

A start-of-message marker is just like a start-of-packet marker, except it consists of 14 distinct characters rather than 4.

Here are the first positions of start-of-message markers for all of the above examples:

  • mjqjpqmgbljsphdztnvjfqwrcgsmlb: first marker after character 19
  • bvwbjplbgvbhsrlpgdmjqwftvncz: first marker after character 23
  • nppdvjthqldpwncqszvftbrmjlhg: first marker after character 23
  • nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg: first marker after character 29
  • zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw: first marker after character 26
defmodule Part2 do
  def solve(input) do
    input
    |> String.to_charlist()
    |> Part1.solve_recursive(14, 0)
  end
end

testInputs
|> Enum.map(&amp;Part2.solve(&amp;1))
Part2.solve(realInput)