Skip to content

Instantly share code, notes, and snippets.

@ignaciovazquez
Last active December 5, 2015 17:22
Show Gist options
  • Save ignaciovazquez/a1c65cf48fe9418fe207 to your computer and use it in GitHub Desktop.
Save ignaciovazquez/a1c65cf48fe9418fe207 to your computer and use it in GitHub Desktop.
Advent of Code. Day 5.
is_nice_string = fn (line) ->
line = String.strip(line)
result = line |> String.codepoints |> Enum.reduce {0, 0, 0, ""}, fn char, {vowels, doubles, invalid, last_char} ->
if last_char == char do
doubles = doubles + 1
end
if char in ["a", "e", "i", "o", "u"] do
vowels = vowels + 1
end
if (last_char <> char) in ["ab", "cd", "pq", "xy"] do
invalid = invalid + 1
end
{vowels, doubles, invalid, char}
end
case result do
{_, _, invalid, _} when invalid > 0 -> {:no, :invalid_chars}
{_, 0, _, _} -> {:no, :no_repeating_chars}
{vowels, _, _, _} when vowels < 3 -> {:no, :not_enough_vowels}
_ -> :yes
end
end
is_nice_string_2 = fn (line) ->
line = String.strip(line)
{pairs, repeating_chars, _, _, _} = line |> String.codepoints |> Enum.reduce {[], 0, "", "", ""}, fn char, {pairs, repeating_chars, last_pair, penultimate_char, last_char} ->
if char == penultimate_char do
repeating_chars = repeating_chars + 1
end
pair = last_char <> char
cond do
last_char != "" and pair != last_pair -> pairs = [ pair | pairs ]
pair == last_pair -> pair = ""
true -> :ok
end
{pairs, repeating_chars, pair, last_char, char}
end
cond do
repeating_chars < 1 -> {:no, :no_repeating_chars}
length(Enum.uniq(pairs)) == length(pairs) -> {:no, :no_repeating_pairs}
true -> :yes
end
end
input_stream = File.stream!("input.txt")
nice_strings = Enum.reduce(input_stream, 0, fn line, acc ->
case is_nice_string.(line) do
:yes -> acc + 1
_ -> acc
end
end)
IO.puts nice_strings
nice_strings_2 = Enum.reduce(input_stream, 0, fn line, acc ->
case is_nice_string_2.(line) do
:yes -> acc + 1
_ -> acc
end
end)
IO.puts nice_strings_2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment