-
Notifications
You must be signed in to change notification settings - Fork 3
/
elixir_day04.ex
60 lines (51 loc) · 1.28 KB
/
elixir_day04.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
48
49
50
51
52
53
54
55
56
57
58
59
60
defmodule ElixirDay04 do
def solve(lower, upper) do
lower..upper
|> Stream.filter(&password?/1)
|> Enum.count()
end
def solve2(lower, upper) do
lower..upper
|> Stream.filter(&password2?/1)
|> Enum.count()
end
def password?(num), do: six_digit?(num) and mono_inc?(num) and part1?(num)
def password2?(num), do: six_digit?(num) and mono_inc?(num) and part2?(num)
def six_digit?(num) when is_integer(num) and num >= 100_000 and num <= 999_999, do: true
def six_digit?(_), do: false
# Monotonically increasing?
def mono_inc?(num) do
Integer.digits(num)
|> Enum.reduce_while(0, fn x, acc ->
if x >= acc do
{:cont, x}
else
{:halt, -1}
end
end)
|> Kernel.>=(0)
end
# group_lengths(123444) = [1, 1, 1, 3]
def group_lengths(num) do
num
|> Integer.digits()
|> Enum.chunk_by(& &1)
|> Enum.map(&length/1)
end
# at least one group >= 2
def part1?(num) do
group_lengths(num)
|> Enum.any?(fn x -> x >= 2 end)
end
# at least one group == 2
def part2?(num) do
group_lengths(num)
|> Enum.any?(fn x -> x == 2 end)
end
def main() do
solve(245_182, 790_572)
|> IO.inspect(label: "Part 1")
solve2(245_182, 790_572)
|> IO.inspect(label: "Part 2")
end
end