Skip to content

Instantly share code, notes, and snippets.

@lud
Created December 6, 2019 21:55
Show Gist options
  • Save lud/5b6243b3fc5495e59821b99c527e9aea to your computer and use it in GitHub Desktop.
Save lud/5b6243b3fc5495e59821b99c527e9aea to your computer and use it in GitHub Desktop.
defmodule Day4 do
def run(range_str) do
[from, to] =
range_str
|> String.split("-")
|> Enum.map(&String.to_integer/1)
Range.new(from, to)
|> Stream.map(&to_charlist/1)
|> Stream.filter(&has_increasing_digits/1)
|> Enum.filter(&has_dup_digits/1)
|> length()
end
def has_increasing_digits([h, d | _]) when h > d,
do: false
def has_increasing_digits([_h, d | t]),
do: has_increasing_digits([d | t])
def has_increasing_digits([_]),
do: true
def remove([char | list], char),
do: remove(list, char)
def remove(list, _),
do: list
def has_dup_digits([h, h, h | t]) do
t
|> remove(h)
|> has_dup_digits()
end
def has_dup_digits([h, h, _ | _]),
do: true
def has_dup_digits([h, h]),
do: true
def has_dup_digits([_h, o | t]),
do: has_dup_digits([o | t])
def has_dup_digits([_]),
do: false
def has_dup_digits([]),
do: false
end
"254032-789860"
|> Day4.run()
|> IO.inspect(label: "Response: ")
System.halt()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment