heck around in elixir

This commit is contained in:
a 2020-12-02 10:58:21 -06:00
parent 95a3d5f8ee
commit aa78a1b463

38
02/02.exs Normal file
View file

@ -0,0 +1,38 @@
regex = ~r{\A(\d+)-(\d+) (\w): (\w+)\z}
list = File.read!("input.txt")
|> String.split("\n")
|> Enum.map(&Regex.run(regex, &1, capture: :all_but_first)) # "all but first" removes matching substring, leaving only capture groups
|> Enum.map(
fn [n1, n2, character, password] ->
{
String.to_integer(n1),
String.to_integer(n2),
character,
String.graphemes(password) # into array of letters
}
end
)
IO.puts "Part 1: "
list
|> Enum.filter(
fn {minimum, maximum, character, password} ->
count = Enum.count(password, &(&1 == character))
minimum <= count and count <= maximum
end
)
|> Enum.count()
|> IO.puts()
IO.puts "Part 2: "
list
|> Enum.filter(
fn {p1, p2, character, password} ->
first = Enum.at(password, p1-1) == character
second = Enum.at(password, p2-1) == character
(first and !second) or (!first and second)
end
)
|> Enum.count()
|> IO.puts()