Created
December 4, 2015 13:20
-
-
Save ignaciovazquez/f643d7e8efe6c6b22d6d to your computer and use it in GitHub Desktop.
Advent of Code. Day 3.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 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