AdventOfCode/2020/day2/day2part2.exs

28 lines
694 B
Elixir
Raw Normal View History

2020-12-02 05:43:05 +00:00
defmodule Day2Part2 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 [first, second, char, password] ->
{
String.to_integer(first) - 1,
String.to_integer(second) - 1,
char,
String.graphemes(password)
}
end)
|> Enum.filter(fn {first, second, char, password} ->
first = Enum.at(password, first) == char
second = Enum.at(password, second) == char
(first && !second) || (!first && second)
end)
|> Enum.count()
|> IO.puts()
end
end
Day2Part2.run()