Day 2 Part 1
This commit is contained in:
parent
da27ab18f7
commit
e8c6100649
2 changed files with 1046 additions and 0 deletions
46
2020/day2/day2.ex
Normal file
46
2020/day2/day2.ex
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
defmodule Day2 do
|
||||
defp read_input do
|
||||
Path.expand('input', Path.dirname(__ENV__.file))
|
||||
|> File.read!()
|
||||
end
|
||||
|
||||
defp parse(input) do
|
||||
re = ~r/^(?<min>\d+)-(?<max>\d+) (?<letter>[a-z]): (?<password>[a-z]+)$/
|
||||
|
||||
String.split(input, "\n", trim: true)
|
||||
|> Enum.map(fn line ->
|
||||
match = Regex.named_captures(re, line)
|
||||
|
||||
%{
|
||||
policy: %{
|
||||
min: String.to_integer(Map.fetch!(match, "min")),
|
||||
max: String.to_integer(Map.fetch!(match, "max")),
|
||||
letter: Map.fetch!(match, "letter")
|
||||
},
|
||||
password: Map.fetch!(match, "password")
|
||||
}
|
||||
end)
|
||||
end
|
||||
|
||||
defp valid?(policy, password) do
|
||||
count =
|
||||
Enum.reduce(String.codepoints(password), 0, fn char, count ->
|
||||
if char == policy.letter do
|
||||
count + 1
|
||||
else
|
||||
count
|
||||
end
|
||||
end)
|
||||
|
||||
policy.min <= count and count <= policy.max
|
||||
end
|
||||
|
||||
def part1 do
|
||||
parse(read_input())
|
||||
|> Enum.filter(fn %{policy: policy, password: password} -> valid?(policy, password) end)
|
||||
|> length()
|
||||
|> IO.inspect()
|
||||
end
|
||||
end
|
||||
|
||||
Day2.part1()
|
||||
Loading…
Reference in a new issue