-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday_04.ex
42 lines (36 loc) · 997 Bytes
/
day_04.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
defmodule AdventOfCode.Y2019.Day04 do
@moduledoc """
--- Day 4: Secure Container ---
Problem Link: https://adventofcode.com/2019/day/4
Difficulty: xs
Tags: sequence inline-input not-fast-enough
"""
def input, do: 245_182..790_572
def run(input \\ input()), do: {run_1(input), run_2(input)}
def run_1(range) do
Enum.count(for n <- range, n |> adjacent() and n |> increasing(), do: n)
end
def run_2(range) do
Enum.count(for n <- range, n |> adjacent_2() and n |> increasing(), do: n)
end
def adjacent(number) do
number
|> Integer.digits()
|> Enum.dedup()
|> Kernel.!=(Integer.digits(number))
end
def increasing(number) do
number
|> Integer.digits()
|> Enum.chunk_every(2, 1, :discard)
|> Enum.map(fn [a, b] -> a <= b end)
|> Enum.all?()
end
def adjacent_2(number) do
number
|> Integer.digits()
|> Enum.chunk_by(& &1)
|> Enum.filter(fn dups -> Enum.count(dups) == 2 end)
|> Enum.any?()
end
end