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

Advent of Code: Day 06

2022/Day06.livemd

Advent of Code: Day 06

Helpers

defmodule Helpers do
  @spec read_file_contents(String.t()) :: binary()
  def read_file_contents(filename) do
    file_path = "/Users/charlie/github.com/charlieroth/advent-of-code/2022/#{filename}"

    case File.read(file_path) do
      {:ok, contents} -> contents
      {:error, _} -> raise("Failed to read file contents")
    end
  end

  @spec in_list(tuple(), list(tuple())) :: boolean()
  def in_list(item, list) do
    Enum.reduce_while(list, false, fn n, acc ->
      if elem(item, 0) === elem(n, 0) do
        {:halt, true}
      else
        {:cont, acc}
      end
    end)
  end
end

Part 01

To be able to communicate with the Elves, the device needs to lock on to their signal. The signal is a series of seemingly-random characters that the device receives one at a time.

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.

Example: 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.

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

How many characters need to be processed before the first start-of-packet marker is detected?

Helpers.read_file_contents("inputs/Day06.txt")
|> String.graphemes()
|> Enum.with_index(1)
|> Enum.chunk_every(4, 1)
|> Enum.filter(fn chunk ->
  Enum.reduce_while(chunk, [], fn item, acc ->
    if Helpers.in_list(item, acc), do: {:halt, false}, else: {:cont, [item | acc]}
  end)
end)
|> Enum.flat_map(&Enum.take(&1, -1))
|> Enum.at(0)

Part 02

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
Helpers.read_file_contents("inputs/Day06.txt")
|> String.graphemes()
|> Enum.with_index(1)
|> Enum.chunk_every(14, 1)
# Find if the chunk contains the same character
# - If the chunk contains duplicate characters, remove this chunk
|> Enum.filter(fn chunk ->
  Enum.reduce_while(chunk, [], fn item, acc ->
    if Helpers.in_list(item, acc), do: {:halt, false}, else: {:cont, [item | acc]}
  end)
end)
# From the remaining chunks take the last element
|> Enum.flat_map(&Enum.take(&1, -1))
# Take the first element, this is the marker and it's index
|> Enum.at(0)

Other Solutions

# https://elixirforum.com/t/advent-of-code-2022-day-6/52286/3

Helpers.read_file_contents("inputs/Day06.txt")
|> String.graphemes()
|> Enum.chunk_every(4, 1, :discard)
|> Enum.reduce_while(0, fn marker, pos ->
  case marker -- Enum.uniq(marker) do
    [] -> {:halt, pos + 4}
    _ -> {:cont, pos + 1}
  end
end)
|> IO.puts()

Helpers.read_file_contents("inputs/Day06.txt")
|> String.graphemes()
|> Enum.chunk_every(14, 1, :discard)
|> Enum.reduce_while(0, fn marker, pos ->
  case marker -- Enum.uniq(marker) do
    [] -> {:halt, pos + 14}
    _ -> {:cont, pos + 1}
  end
end)
|> IO.puts()