Skip to content

Instantly share code, notes, and snippets.

@EricDykstra
Created December 3, 2015 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EricDykstra/f7a6efd50c887b015483 to your computer and use it in GitHub Desktop.
Save EricDykstra/f7a6efd50c887b015483 to your computer and use it in GitHub Desktop.
Advent Of Code Day 3
defmodule PresentDelivery do
def input do
"^><^>>>^"
end
def main(_) do
input
|> String.codepoints
|> split_in_two
|> Enum.map(&santa_path/1)
|> Enum.concat
|> Enum.uniq
|> Enum.count
|> IO.puts
end
def split_in_two(list) do
santa = Enum.take_every(list, 2)
[_ | newlist] = list
robo = Enum.take_every(newlist, 2)
[santa, robo]
end
def santa_path(input) do
input
|> Enum.reduce([[0,0]], fn(dir, acc) -> [next_point(List.first(acc), dir) | acc ] end)
end
def next_point(current, direction) do
[x, y] = current
case direction do
"^" -> [x+1, y]
"v" -> [x-1, y]
"<" -> [x, y-1]
">" -> [x, y+1]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment