Skip to content

Instantly share code, notes, and snippets.

@boone
Created December 7, 2015 01:03
Show Gist options
  • Save boone/7a8eb7d24f1b90595005 to your computer and use it in GitHub Desktop.
Save boone/7a8eb7d24f1b90595005 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_by_santa(instructions):
visited = [[0, 0]]
x = y = 0
for move in list(instructions):
if move == "^": # north
y += 1
elif move == "v": # south
y -= 1
elif move == ">": # east
x += 1
elif move == "<": # west
x -= 1
position = [x, y]
if not position in visited:
visited.append(position)
return len(visited)
# 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 }
for i, move in enumerate(list(instructions)):
if i % 2 == 0:
person = santa
else:
person = robot
if move == "^": # north
person['y'] += 1
elif move == "v": # south
person['y'] -= 1
elif move == ">": # east
person['x'] += 1
elif move == "<": # west
person['x'] -= 1
position = [person['x'], person['y']]
if not position in visited:
visited.append(position)
return len(visited)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment