-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday_02.ex
47 lines (38 loc) · 1.16 KB
/
day_02.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
defmodule AdventOfCode.Y2020.Day02 do
@moduledoc """
--- Day 2: Password Philosophy ---
Problem Link: https://adventofcode.com/2020/day/2
"""
alias AdventOfCode.Helpers.{InputReader, Transformers}
def input, do: InputReader.read_from_file(2020, 2)
def run(input \\ input()) do
input = parse(input)
{solve(input), solve_corrected(input)}
end
def parse(input) do
input
|> Transformers.lines()
|> Enum.map(&parse_line/1)
end
@pattern ~r/(\d+)-(\d+) (\S): (\S+)$/
defp parse_line(line) do
[_, lo, hi, char, pass] = Regex.run(@pattern, line)
{String.to_integer(lo), String.to_integer(hi), char, pass}
end
defp solve(lines), do: Enum.count(lines, &valid?/1)
defp solve_corrected(lines), do: Enum.count(lines, &valid_position?/1)
defp valid?({lo, hi, char, pass}) do
pass
|> String.graphemes()
|> Enum.frequencies()
|> Map.get(char)
|> then(fn freq -> freq >= lo and freq <= hi end)
end
defp valid_position?({lo, hi, char, pass}) do
case {String.at(pass, lo - 1), String.at(pass, hi - 1)} do
{a, a} -> false
{a, b} when a == char or b == char -> true
_ -> false
end
end
end