Skip to content

Daniel-Xu/advent-of-code

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AdventOfCode

2016

Skill required to solve AOC 2016:

  1. BFS is the king (or you can use A *)
  2. Binary pattern matching
  3. recursive thinking
  4. bit operation
  5. File operation (Stream)
  6. Regex
  7. Some erlang data structures (mostly :queue)

2017

Key point for each problem

  1. For wrap around, you will want to use rem(i, len)

  2. Enum.max, Enum.min or Enum.min_max

  3. 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---> ...
  1. (String.codepoints or String.split) |> Enum.sort()

  2. Use %{} not [] for the O(1) happiness

  3. List.update_at, List.replace_at and MapSet.member?

  4. DFS

  5. String.split(s, " ")

  6. Regex.scan

  7. 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
  1. Hex grid: {x, y} => {x (+/-) 0.5 or 1, y (+/-) 0.5 or 1}

  2. DFS

  3. Each cycle will have (range - 1) * 2 number of elements

  4. DFS

  5. mask = (1 <<< len) - 1 to generate len number of 1 mask

  6. 1 billion times is impossible (find cycyle)

  7. Similar with Day1, wrap around: rem(i, len)

  8. Run two Map together: process([p0, p0_i], [p1, p1_i])

  9. Convert List to Map:

[[1, 2], [3, 4]]

# to

%{
  0 => %{0 => 1, 1 => 2}
  1 => %{0 => 3, 1 => 4}
}
  1. %{ m | 0 => "new value" } and Enum.uniq_by

  2. 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()
  1. Using {dx, dy} to control direction

  2. 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
  1. DFS

  2. Pattern Matching

About

My solution to advent of code using Elixir

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages