-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.ex
104 lines (84 loc) · 2.08 KB
/
day11.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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
defmodule Y2015.Day11 do
use Advent.Day, no: 11
@input "hxbxwxba"
@doc """
iex> Day11.part1("abcdefgh")
"abcdffaa"
iex> Day11.part1("ghijklmn")
"ghjaabcc"
"""
def part1(input \\ @input) do
input
|> cook
|> do_part1
|> uncook
end
defp do_part1(chars) do
if valid_password?(chars) do
chars
else
# Increment and try again...
chars
|> increment_char
|> do_part1
end
end
def cook(string) do
string
|> String.reverse()
|> String.to_charlist()
end
def uncook(chars) do
chars
|> IO.chardata_to_string()
|> String.reverse()
end
@doc """
iex> Day11.increment("abcdefgh")
"abcdefgi"
iex> Day11.increment("abcdefgz")
"abcdefha"
iex> Day11.increment("abcdzzzz")
"abceaaaa"
"""
def increment(input) do
input
|> cook
|> increment_char
|> uncook
end
defp increment_char([?z | rest]), do: [?a | increment_char(rest)]
defp increment_char([notz | rest]), do: [notz + 1 | rest]
@doc """
iex> "hijklmmn" |> Day11.cook |> Day11.valid_password?
false
iex> "abbceffg" |> Day11.cook |> Day11.valid_password?
false
iex> "abbcegjk" |> Day11.cook |> Day11.valid_password?
false
iex> "abcdffaa" |> Day11.cook |> Day11.valid_password?
true
iex> "ghjaabcc" |> Day11.cook |> Day11.valid_password?
true
"""
def valid_password?(chars) do
!forbidden_letters?(chars) && double_letters?(chars, 0) && has_sequence?(chars)
end
defp double_letters?(_, 2), do: true
defp double_letters?([x, x | rest], count), do: double_letters?(rest, count + 1)
defp double_letters?([_, y | rest], count), do: double_letters?([y | rest], count)
defp double_letters?(_, _), do: false
defp forbidden_letters?(chars) do
Enum.any?(chars, fn char -> char == ?i || char == ?o || char == ?l end)
end
defp has_sequence?([z, y, x | rest]) do
if y == x + 1 && z == y + 1 do
true
else
has_sequence?([y, x | rest])
end
end
defp has_sequence?(_), do: false
def part1_verify, do: part1()
def part2_verify, do: part1() |> increment() |> part1()
end