-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday21.ex
196 lines (163 loc) · 4.91 KB
/
day21.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
defmodule Elixir2016.Day21 do
def parse(filename) do
File.stream!(filename)
|> Stream.map(&String.trim/1)
|> Stream.map(&parse_line/1)
|> Enum.to_list()
end
def parse_line(line) do
move = ~r/move position (\d+) to position (\d+)/
reverse = ~r/reverse positions (\d+) through (\d+)/
swap_letter = ~r/swap letter (\w+) with letter (\w+)/
swap_pos = ~r/swap position (\d+) with position (\d+)/
rotate = ~r/rotate (left|right) (\d+) step/
rotate_pos = ~r/rotate based on position of letter (\w+)/
cond do
Regex.match?(move, line) ->
[arg1, arg2] = Regex.run(move, line) |> Enum.drop(1) |> Enum.map(&String.to_integer/1)
{:move, arg1, arg2}
Regex.match?(reverse, line) ->
[arg1, arg2] = Regex.run(reverse, line) |> Enum.drop(1) |> Enum.map(&String.to_integer/1)
{:reverse, arg1, arg2}
Regex.match?(rotate_pos, line) ->
[_, arg1] = Regex.run(rotate_pos, line)
{:rotate_pos, arg1}
Regex.match?(rotate, line) ->
[_, arg1, arg2] = Regex.run(rotate, line)
{:rotate, arg1, String.to_integer(arg2)}
Regex.match?(swap_letter, line) ->
[_, arg1, arg2] = Regex.run(swap_letter, line)
{:swap_letter, arg1, arg2}
Regex.match?(swap_pos, line) ->
[arg1, arg2] = Regex.run(swap_pos, line) |> Enum.drop(1) |> Enum.map(&String.to_integer/1)
{:swap_pos, arg1, arg2}
true ->
line
end
end
def part1(filename) do
parse(filename)
|> run_commands("abcdefgh")
end
def part2(filename) do
commands = parse(filename)
"abcdefgh"
|> String.graphemes()
|> permutations()
|> Enum.map(&Enum.join/1)
|> Stream.filter(fn passwd ->
run_commands(commands, passwd) == "fbgdceah"
end)
|> Enum.take(1)
end
def run_commands(commands, initial_string) do
commands
|> Enum.reduce(initial_string, fn command, acc ->
run_command(command, acc)
end)
end
def run_command({:swap_pos, pos1, pos2}, string) do
char1 = String.at(string, pos1)
char2 = String.at(string, pos2)
string
|> string_update(pos1, char2)
|> string_update(pos2, char1)
end
def run_command({:swap_letter, let1, let2}, string) do
string
|> String.graphemes()
|> Enum.map(fn this_letter ->
case this_letter do
^let1 -> let2
^let2 -> let1
_ -> this_letter
end
end)
|> Enum.join()
end
def run_command({:reverse, pos1, pos2}, string) when pos2 > pos1 do
# "abcdef": Reverse {2, 4}
# 0-1 "ab" left
# 2-4 "cde" mid
# 5 "f" right
{left_mid, right} = String.split_at(string, pos2 + 1)
{left, mid} = String.split_at(left_mid, pos1)
left <> String.reverse(mid) <> right
end
def run_command({:rotate, "left", n}, string) do
string
|> String.graphemes()
|> rotate_left(n)
|> Enum.join()
end
def run_command({:rotate, "right", n}, string) do
string
|> String.graphemes()
|> rotate_right(n)
|> Enum.join()
end
def run_command({:move, pos1, pos2}, string) do
list = string |> String.graphemes()
char1 = list |> Enum.at(pos1)
list
|> List.delete_at(pos1)
|> List.insert_at(pos2, char1)
|> Enum.join()
end
# "rotate based on position of letter b" finds the index of letter b (1), then
# rotates the string right once plus a number of times equal to that index (2):
# ecabd.
# Once the index is determined, rotate the string to the right one time, plus a
# number of times equal to that index, plus one additional time if the index was
# at least 4.
def run_command({:rotate_pos, char1}, string) do
[left, _] = String.split(string, char1, parts: 2)
pos1 = String.length(left)
rotate_amount =
cond do
pos1 >= 4 -> pos1 + 2
true -> pos1 + 1
end
run_command({:rotate, "right", rotate_amount}, string)
end
def run_command(_command, string), do: string
### Helpers ###
@doc """
iex(3)> Day21.string_update("abcdef", 3, "Z")
"abcZef"
"""
def string_update(string, position, char) do
# iex(4)> "abcdef" |> String.at(3)
# "d"
{left, right} = String.split_at(string, position)
if String.length(right) == 1 do
left <> char
else
{_replaced, trunc_right} = String.split_at(right, 1)
left <> char <> trunc_right
end
end
@doc """
iex(2)> Day21.rotate_left([5, 6, 7, 8, 9], 2)
[7, 8, 9, 5, 6]
"""
def rotate_left(list, 0), do: list
def rotate_left([h | t], n) when n > 0 do
(t ++ [h])
|> rotate_left(n - 1)
end
@doc """
iex(8)> Day21.rotate_right([5, 6, 7, 8, 9], 2)
[8, 9, 5, 6, 7]
"""
def rotate_right(list, 0), do: list
def rotate_right(list, n) when n > 0 do
list
|> Enum.reverse()
|> rotate_left(n)
|> Enum.reverse()
end
def permutations([]), do: [[]]
def permutations(list),
do: for(elem <- list, rest <- permutations(list -- [elem]), do: [elem | rest])
end