Run this notebook

Use Livebook to open this notebook and explore new ideas.

It is easy to get started, on your machine or the cloud.

Click below to open and run it in your Livebook at .

(or change your Livebook location)

# 2022 Day 6 ```elixir Mix.install([:kino]) ``` ## Input [![Run in Livebook](https://livebook.dev/badge/v1/black.svg)](https://livebook.dev/run?url=https%3A%2F%2Fgithub.com%2FballPointPenguin%2Faoc2022%2Fblob%2Fmain%2Fday_06.livemd) ```elixir input = Kino.Input.textarea("Puzzle Input") ``` ## Code <h3> Module </h3> ```elixir defmodule Tuning do def start_marker(input), do: find_marker(input, 4) def message_marker(input), do: find_marker(input, 14) defp find_marker(input, length) do input |> process_input() |> Enum.chunk_every(length, 1, :discard) |> Enum.find_index(fn frame -> Enum.uniq(frame) == frame end) |> Kernel.+(length) end defp process_input(input), do: String.to_charlist(input) end ``` <h3> Evaluate </h3> ```elixir part_1 = input |> Kino.Input.read() |> Tuning.start_marker() part_2 = input |> Kino.Input.read() |> Tuning.message_marker() {part_1, part_2} ``` <h3> Test </h3> <!-- livebook:{"reevaluate_automatically":true} --> ```elixir ExUnit.start(autorun: false) defmodule TuningTest do use ExUnit.Case, async: true setup do data_streams = [ "mjqjpqmgbljsphdztnvjfqwrcgsmlb", "bvwbjplbgvbhsrlpgdmjqwftvncz", "nppdvjthqldpwncqszvftbrmjlhg", "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg", "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw" ] {:ok, data_streams: data_streams} end test "start_marker", %{data_streams: data_streams} do assert Tuning.start_marker(Enum.at(data_streams, 0)) == 7 assert Tuning.start_marker(Enum.at(data_streams, 1)) == 5 assert Tuning.start_marker(Enum.at(data_streams, 2)) == 6 assert Tuning.start_marker(Enum.at(data_streams, 3)) == 10 assert Tuning.start_marker(Enum.at(data_streams, 4)) == 11 end test "message_marker", %{data_streams: data_streams} do assert Tuning.message_marker(Enum.at(data_streams, 0)) == 19 assert Tuning.message_marker(Enum.at(data_streams, 1)) == 23 assert Tuning.message_marker(Enum.at(data_streams, 2)) == 23 assert Tuning.message_marker(Enum.at(data_streams, 3)) == 29 assert Tuning.message_marker(Enum.at(data_streams, 4)) == 26 end end ExUnit.run() ```
See source

Have you already installed Livebook?

If you already installed Livebook, you can configure the default Livebook location where you want to open notebooks.
Livebook up Checking status We can't reach this Livebook (but we saved your preference anyway)
Run notebook

Not yet? Install Livebook in just a minute

Livebook is open source, free, and ready to run anywhere.

Run on your machine

with Livebook Desktop

Run in the cloud

on select platforms

To run on Linux, Docker, embedded devices, or Elixir’s Mix, check our README.

PLATINUM SPONSORS
SPONSORS
Code navigation with go to definition of modules and functions Read More ×