AdventOfCode/2020/elixir/day2/day2part1.exs

22 lines
585 B
Elixir
Raw Normal View History

2020-12-02 05:18:45 +00:00
defmodule Day2Part1 do
def run do
regex = ~r/\A(\d+)-(\d+) (\w): (\w+)\z/
File.read!("input")
|> String.trim()
|> String.split("\n")
|> Enum.map(&Regex.run(regex, &1, capture: :all_but_first))
|> Enum.map(fn [least, most, char, password] ->
{String.to_integer(least), String.to_integer(most), char, password}
end)
|> Enum.filter(fn {least, most, char, password} ->
count = Enum.count(String.graphemes(password), &(&1 == char))
least <= count && count <= most
end)
|> Enum.count()
|> IO.puts()
end
end
Day2Part1.run()