blob: 1801b009e4d274a8e5ecebdad09c98298e558878 (
plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
start = String.trim(File.read!("inputs/day11.txt"))
startCharlist = String.to_charlist(start)
startNums = Enum.reverse(startCharlist)
defmodule A do
def incrementPassword([head | tail]) do
if decreasingStraight?(tail) do
simpleIncrementPassword([head | tail])
else
[second | _] = tail
if head < second && second < ?z && !(second >= ?h && second <= ?n) do
[second + 1 | tail]
else
[?a | simpleIncrementPassword(tail)]
end
end
end
def simpleIncrementPassword([?z | tail]) do
[?a | simpleIncrementPassword(tail)]
end
def simpleIncrementPassword([?h | tail]) do
[?j | tail]
end
def simpleIncrementPassword([?n | tail]) do
[?p | tail]
end
def simpleIncrementPassword([?k | tail]) do
[?m | tail]
end
def simpleIncrementPassword([head | tail]) do
[head + 1 | tail]
end
def decreasingStraight?([]) do
false
end
def decreasingStraight?([_]) do
false
end
def decreasingStraight?([_, _]) do
false
end
def decreasingStraight?([a, b, c | _]) when b === a - 1 and c === a - 2 do
true
end
def decreasingStraight?([_ | tail]) do
decreasingStraight?(tail)
end
def twoDifferentPairs?(list) do
differentPairs =
Enum.zip(list, Enum.drop(list, 1))
|> Enum.flat_map(fn
{a, a} -> [a]
{_, _} -> []
end)
|> Enum.dedup()
|> Enum.count()
differentPairs >= 2
end
end
validPasswords =
Stream.iterate(startNums, &A.incrementPassword(&1))
|> Stream.filter(fn password ->
A.decreasingStraight?(password) &&
A.twoDifferentPairs?(password)
end)
nextPasswords =
Enum.take(validPasswords, 2)
|> Enum.map(&Enum.reverse(&1))
|> Enum.join("\n")
IO.puts("New passwords:\n#{nextPasswords}")
|