DEV Community

Discussion on: Advent of Code 2019 Solution Megathread - Day 4: Secure Container

Collapse
 
yordiverkroost profile image
Yordi Verkroost

My solutions in Elixir. I'm just posting the final solution for part two since that essentially contains the solution for part one as well. A very nice day to use the power of pattern-matching!

defmodule Aoc19.Day4b do
  @moduledoc false

  def start do
    {lower, upper} = read()

    check(lower, upper, 0)
  end

  defp check(current, upper, count) when current <= upper do
    with :ok <- adjacent?(current),
         :ok <- increasing?(current) do
      check(current + 1, upper, count + 1)
    else
      _ -> check(current + 1, upper, count)
    end
  end

  defp check(_, _, count), do: count

  defp increasing?(current) do
    current
    |> Integer.to_string()
    |> String.graphemes()
    |> is_increasing?()
  end

  defp is_increasing?([]), do: :ok
  defp is_increasing?([_]), do: :ok

  defp is_increasing?([h1, h2 | rest]) when h1 <= h2 do
    is_increasing?([h2 | rest])
  end

  defp is_increasing?(_), do: :error

  defp adjacent?(current) do
    current
    |> Integer.to_string()
    |> String.graphemes()
    |> is_adjacent?()
  end

  defp is_adjacent?([]), do: :error

  defp is_adjacent?([h1, h1, h1 | rest]) do
    rest = skip(h1, rest)
    is_adjacent?(rest)
  end

  defp is_adjacent?([h1, h1 | _rest]), do: :ok
  defp is_adjacent?([_h1 | rest]), do: is_adjacent?(rest)

  defp skip(h, [h | rest]), do: skip(h, rest) 
  defp skip(_h, rest), do: rest

  defp read, do: {231_832, 767_346}
end