diff options
author | Oscar Najera <hi@oscarnajera.com> | 2022-12-06 17:26:18 +0100 |
---|---|---|
committer | Oscar Najera <hi@oscarnajera.com> | 2022-12-06 17:26:18 +0100 |
commit | 7ca1851f732e2f74339354ecc25ef709754a3ac3 (patch) | |
tree | 4cb8db92d5024947edae833b250f749c423bd9be /AoC2022/06/solver.exs | |
parent | f34c85b78a1cbefa475a252184c13b68c9727332 (diff) | |
download | scratch-7ca1851f732e2f74339354ecc25ef709754a3ac3.tar.gz scratch-7ca1851f732e2f74339354ecc25ef709754a3ac3.tar.bz2 scratch-7ca1851f732e2f74339354ecc25ef709754a3ac3.zip |
[AoC2022] 06 Rust & Elixir
Diffstat (limited to 'AoC2022/06/solver.exs')
-rw-r--r-- | AoC2022/06/solver.exs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/AoC2022/06/solver.exs b/AoC2022/06/solver.exs new file mode 100644 index 0000000..3d47677 --- /dev/null +++ b/AoC2022/06/solver.exs @@ -0,0 +1,27 @@ +defmodule Solver.Day6 do + def marker([_h | tail] = line, distinct, counter) do + line + |> Enum.take(distinct) + |> MapSet.new() + |> MapSet.size() + |> Kernel.then(fn amount -> + case amount do + ^distinct -> counter + _ -> marker(tail, distinct, counter + 1) + end + end) + end + + def start(msg, distinct) do + String.graphemes(msg) |> marker(distinct, distinct) |> IO.puts() + end +end + +case File.read("input") do + {:ok, body} -> + Solver.Day6.start(body, 4) + Solver.Day6.start(body, 14) + + {:error, reason} -> + IO.puts(reason) +end |