87 lines
1.4 KiB
Text
87 lines
1.4 KiB
Text
|
# AOC 2022 - Day 6
|
||
|
|
||
|
```elixir
|
||
|
Mix.install([
|
||
|
{:req, "~> 0.3.3"}
|
||
|
])
|
||
|
```
|
||
|
|
||
|
## Puzzle description
|
||
|
|
||
|
[Day 6: Tuning Trouble](https://adventofcode.com/2022/day/6).
|
||
|
|
||
|
## Input
|
||
|
|
||
|
```elixir
|
||
|
defmodule Load do
|
||
|
def input do
|
||
|
aoc_session = System.fetch_env!("LB_AOC_SESSION")
|
||
|
input_url = "https://adventofcode.com/2022/day/6/input"
|
||
|
Req.get!(input_url, headers: [cookie: "session=#{aoc_session}"]).body
|
||
|
end
|
||
|
end
|
||
|
```
|
||
|
|
||
|
## Solution
|
||
|
|
||
|
```elixir
|
||
|
defmodule Marker do
|
||
|
def find(input, marker_length) do
|
||
|
input
|
||
|
|> String.graphemes()
|
||
|
|> Enum.chunk_every(marker_length, 1)
|
||
|
|> Enum.map(&MapSet.new/1)
|
||
|
|> Enum.find_index(&(MapSet.size(&1) == marker_length))
|
||
|
|> Kernel.+(marker_length)
|
||
|
end
|
||
|
end
|
||
|
```
|
||
|
|
||
|
```elixir
|
||
|
defmodule Part1 do
|
||
|
@marker_length 4
|
||
|
def run(input) do
|
||
|
input
|
||
|
|> Marker.find(@marker_length)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
defmodule Part2 do
|
||
|
@marker_length 14
|
||
|
def run(input) do
|
||
|
input
|
||
|
|> Marker.find(@marker_length)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
ExUnit.start(autorun: false)
|
||
|
|
||
|
defmodule Test do
|
||
|
use ExUnit.Case, async: true
|
||
|
@example_input ~s(mjqjpqmgbljsphdztnvjfqwrcgsmlb)
|
||
|
@input Load.input()
|
||
|
|
||
|
test "it loads the input" do
|
||
|
assert String.length(@input) > 0
|
||
|
end
|
||
|
|
||
|
test "part 1 example" do
|
||
|
assert Part1.run(@example_input) === 7
|
||
|
end
|
||
|
|
||
|
test "part 1" do
|
||
|
assert Part1.run(@input) === 1093
|
||
|
end
|
||
|
|
||
|
test "part 2 example" do
|
||
|
assert Part2.run(@example_input) === 19
|
||
|
end
|
||
|
|
||
|
test "part 2" do
|
||
|
assert Part2.run(@input) === 3534
|
||
|
end
|
||
|
end
|
||
|
|
||
|
ExUnit.run()
|
||
|
```
|