aoc/22/lib/days/day6.ex

30 lines
519 B
Elixir
Raw Normal View History

2023-04-17 18:18:20 +00:00
defmodule AOC.Day6 do
use AOC.Day, day: 6
def parse_input(lines), do: lines |> hd() |> String.to_charlist()
def part1(input) do
find_marker(input, 0, 4)
end
def part2(input) do
find_marker(input, 0, 14)
end
def find_marker(signal, index, length) do
distinct =
signal
|> Enum.take(length)
|> MapSet.new()
|> Enum.count()
if distinct == length do
index + length
else
signal
|> tl()
|> find_marker(index + 1, length)
end
end
end