Skill required to solve AOC 2016:
- BFS is the king (or you can use A *)
- Binary pattern matching
- recursive thinking
- bit operation
- File operation (Stream)
- Regex
- Some erlang data structures (mostly
:queue
)
Key point for each problem
-
For wrap around, you will want to use
rem(i, len)
-
Enum.max
,Enum.min
orEnum.min_max
-
Learn the spiral move, this is so important.
Odd: right: 1, up: n, left: n
17 16 15 14 13
18 5 * * 12
19 6 * > * 11
20 7 8 9 10
21 22 23---> ...
Even: left: 1, down: n, right: n
17 16 15 14 13
18 ^ < 4 3 12
19 ^ 1 2 11
20 ^ ^ ^ 10
21 22 23---> ...
-
(String.codepoints or String.split) |> Enum.sort()
-
Use
%{}
not[]
for theO(1)
happiness -
List.update_at
,List.replace_at
andMapSet.member?
-
DFS
-
String.split(s, " ")
-
Regex.scan
-
List rotate left/right (
Enum.split
)
defp rotate_left(l, n) do
len = Enum.count(l)
{h, t} = Enum.split(l, rem(n, len))
t ++ h
end
defp rotate_right(l, n) do
len = Enum.count(l)
{h, t} = Enum.split(l, rem(len - n, len))
t ++ h
end
-
Hex grid:
{x, y} => {x (+/-) 0.5 or 1, y (+/-) 0.5 or 1}
-
DFS
-
Each cycle will have
(range - 1) * 2
number of elements -
DFS
-
mask = (1 <<< len) - 1
to generatelen
number of 1 mask -
1 billion times is impossible (find cycyle)
-
Similar with Day1, wrap around:
rem(i, len)
-
Run two
Map
together:process([p0, p0_i], [p1, p1_i])
-
Convert List to Map:
[[1, 2], [3, 4]]
# to
%{
0 => %{0 => 1, 1 => 2}
1 => %{0 => 3, 1 => 4}
}
-
%{ m | 0 => "new value" }
andEnum.uniq_by
-
Matrix Rotate, Flip, Transpose
def transpose(matrix) do
List.zip(matrix) |> Enum.map(&Tuple.to_list/1)
end
def flip_x(matrix), do: Enum.reverse(matrix)
def flip_y(matrix), do: for x <- matrix, do: Enum.reverse(x)
def rotate(matrix), do: transpose(matrix) |> flip_y()
-
Using
{dx, dy}
to control direction -
How to check if N is prime number
defp prime_n?(x) do
Enum.reduce_while(2..div(x, 2), true, fn(d, acc) ->
if rem(x, d) == 0, do: {:halt, false}, else: {:cont, acc}
end)
end
-
DFS
-
Pattern Matching