Skip to content

Instantly share code, notes, and snippets.

@boone
Created December 5, 2015 21:11
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 boone/327db28f7599a617e255 to your computer and use it in GitHub Desktop.
Save boone/327db28f7599a617e255 to your computer and use it in GitHub Desktop.
# http://adventofcode.com/day/3 - part 1
#
# instructions - String with sequentials commands for which direction to take.
# ^ north, v south, > east, < west
#
# Returns an integer of the area of wrapping paper required, in square feet.
def houses_visited(instructions)
visited = [[0, 0]]
x = 0
y = 0
instructions.split("").each do |move|
if move == "^" # north
y += 1
elsif move == "v" # south
y -= 1
elsif move == ">" # east
x += 1
elsif move == "<" # west
x -= 1
end
position = [x, y]
visited << position unless visited.include?(position)
end
visited.size
end
# http://adventofcode.com/day/3 - part 2
#
# instructions - String with sequentials commands for which direction to take.
# ^ north, v south, > east, < west
#
# Returns an integer of the area of wrapping paper required, in square feet.
def houses_visited_by_santa_and_robot(instructions)
visited = [[0, 0]]
santa = { x: 0, y: 0 }
robot = { x: 0, y: 0 }
instructions.split("").each_with_index do |move, i|
person = i.even? ? santa : robot
if move == "^" # north
person[:y] += 1
elsif move == "v" # south
person[:y] -= 1
elsif move == ">" # east
person[:x] += 1
elsif move == "<" # west
person[:x] -= 1
end
position = [person[:x], person[:y]]
visited << position unless visited.include?(position)
end
visited.size
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment