Watch
1
0
Fork
You've already forked advent-of-code
0

Day 2 Part 1

This commit is contained in:
Jeremy Kaplan 2020-12-01 23:46:41 -08:00
commit e8c6100649
2 changed files with 1046 additions and 0 deletions

46
2020/day2/day2.ex Normal file
View 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()