Skip to content

Instantly share code, notes, and snippets.

@ignaciovazquez
Created December 4, 2015 13:20
Show Gist options
  • Save ignaciovazquez/f643d7e8efe6c6b22d6d to your computer and use it in GitHub Desktop.
Save ignaciovazquez/f643d7e8efe6c6b22d6d to your computer and use it in GitHub Desktop.
Advent of Code. Day 3.
### Part 1
move = fn x, y, dir ->
case dir do
">" -> {x + 1, y}
"<" -> {x - 1, y}
"^" -> {x, y + 1}
"v" -> {x, y - 1}
end
end
{_, _, set} = input |> String.codepoints |> Enum.reduce {0, 0, HashSet.new}, fn (dir, {x, y, set}) ->
{x, y} = move.(x, y, dir)
{x, y, HashSet.put(set, {x, y})}
end
IO.puts "Houses visited: #{HashSet.size set}"
### Part 2
{_, _, _, _, _, set} = input |> String.codepoints |> Enum.reduce {0, 0, 0, 0, :santa, HashSet.new}, fn (dir, {santa_x, santa_y, robo_x, robo_y, who, set}) ->
case who do
:santa ->
{santa_x, santa_y} = move.(santa_x, santa_y, dir)
{santa_x, santa_y, robo_x, robo_y, :robo, HashSet.put(set, {santa_x, santa_y})}
:robo ->
{robo_x, robo_y} = move.(robo_x, robo_y, dir)
{santa_x, santa_y, robo_x, robo_y, :santa, HashSet.put(set, {robo_x, robo_y})}
end
end
IO.puts "Houses visited (santa + robot): #{HashSet.size set}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment