Add solutions for 2022:6 Tuning Trouble
This commit is contained in:
parent
c98303d0c1
commit
67ff3997f1
2 changed files with 88 additions and 0 deletions
43
2022-elixir/lib/solutions/day_06.ex
Normal file
43
2022-elixir/lib/solutions/day_06.ex
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
defmodule Aoc.Solution.Day06 do
|
||||||
|
@name "Day 6: Tuning Trouble"
|
||||||
|
@behaviour Solution
|
||||||
|
|
||||||
|
@impl Solution
|
||||||
|
def get_name, do: @name
|
||||||
|
|
||||||
|
@impl Solution
|
||||||
|
def present(solution), do: "Packet marker is at #{solution}"
|
||||||
|
|
||||||
|
@impl Solution
|
||||||
|
def present_again(solution), do: "Message marker is at #{solution}"
|
||||||
|
|
||||||
|
@impl Solution
|
||||||
|
def parse!(raw) do
|
||||||
|
raw |> String.trim() |> String.codepoints()
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl Solution
|
||||||
|
def solve(datastream) do
|
||||||
|
datastream |> packet_marker()
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl Solution
|
||||||
|
def solve_again(datastream) do
|
||||||
|
datastream |> message_marker()
|
||||||
|
end
|
||||||
|
|
||||||
|
def packet_marker(datastream) do
|
||||||
|
distinct_sequence(datastream, 4)
|
||||||
|
end
|
||||||
|
|
||||||
|
def message_marker(datastream) do
|
||||||
|
distinct_sequence(datastream, 14)
|
||||||
|
end
|
||||||
|
|
||||||
|
def distinct_sequence(datastream, l, start \\ 0) do
|
||||||
|
case datastream |> Enum.slice(start, l) |> MapSet.new() |> MapSet.size() do
|
||||||
|
^l -> start + l
|
||||||
|
_ -> distinct_sequence(datastream, l, start + 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
45
2022-elixir/test/solutions/day_06_test.exs
Normal file
45
2022-elixir/test/solutions/day_06_test.exs
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
defmodule Day06Test do
|
||||||
|
use ExUnit.Case
|
||||||
|
doctest Aoc.Solution.Day06
|
||||||
|
import Aoc.Solution.Day06
|
||||||
|
|
||||||
|
@input [
|
||||||
|
"mjqjpqmgbljsphdztnvjfqwrcgsmlb",
|
||||||
|
"bvwbjplbgvbhsrlpgdmjqwftvncz",
|
||||||
|
"nppdvjthqldpwncqszvftbrmjlhg",
|
||||||
|
"nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg",
|
||||||
|
"zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw"
|
||||||
|
]
|
||||||
|
|
||||||
|
test "06: Tuning Trouble, part 1" do
|
||||||
|
[input_a, input_b, input_c, input_d, input_e] = @input
|
||||||
|
|
||||||
|
result_a = input_a |> parse!() |> solve()
|
||||||
|
result_b = input_b |> parse!() |> solve()
|
||||||
|
result_c = input_c |> parse!() |> solve()
|
||||||
|
result_d = input_d |> parse!() |> solve()
|
||||||
|
result_e = input_e |> parse!() |> solve()
|
||||||
|
|
||||||
|
assert result_a == 7
|
||||||
|
assert result_b == 5
|
||||||
|
assert result_c == 6
|
||||||
|
assert result_d == 10
|
||||||
|
assert result_e == 11
|
||||||
|
end
|
||||||
|
|
||||||
|
test "06: Tuning Trouble, part 2" do
|
||||||
|
[input_a, input_b, input_c, input_d, input_e] = @input
|
||||||
|
|
||||||
|
result_a = input_a |> parse!() |> solve_again()
|
||||||
|
result_b = input_b |> parse!() |> solve_again()
|
||||||
|
result_c = input_c |> parse!() |> solve_again()
|
||||||
|
result_d = input_d |> parse!() |> solve_again()
|
||||||
|
result_e = input_e |> parse!() |> solve_again()
|
||||||
|
|
||||||
|
assert result_a == 19
|
||||||
|
assert result_b == 23
|
||||||
|
assert result_c == 23
|
||||||
|
assert result_d == 29
|
||||||
|
assert result_e == 26
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Add table
Reference in a new issue