Advent Of Code 2015: Day 3


The Challenge

--- Day 3: Perfectly Spherical Houses in a Vacuum ---

Santa is delivering presents to an infinite two-dimensional grid of houses.

He begins by delivering a present to the house at his starting location, and then an elf at the North Pole calls him via radio and tells him where to move next. Moves are always exactly one house to the north (^), south (v), east (>), or west (<). After each move, he delivers another present to the house at his new location.

However, the elf back at the north pole has had a little too much eggnog, and so his directions are a little off, and Santa ends up visiting some houses more than once. How many houses receive at least one present?

For example:

> delivers presents to 2 houses: one at the starting location, and one to the east.
^>v< delivers presents to 4 houses in a square, including twice to the house at his starting/ending location.
^v^v^v^v^v delivers a bunch of presents to some very lucky children at only 2 houses.
--- Part Two ---

The next year, to speed up the process, Santa creates a robot version of himself, Robo-Santa, to deliver presents with him.

Santa and Robo-Santa start at the same location (delivering two presents to the same starting house), then take turns moving based on instructions from the elf, who is eggnoggedly reading from the same script as the previous year.

This year, how many houses receive at least one present?

For example:

^v delivers presents to 3 houses, because Santa goes north, and then Robo-Santa goes south.
^>v< now delivers presents to 3 houses, and Santa and Robo-Santa end up back where they started.
^v^v^v^v^v now delivers presents to 11 houses, with Santa going one direction and Robo-Santa going the other.

Solution

A rather interesting challenge. I wasn't too happy with my solution but it works.
I split up parts 1 and parts 2 so the code can be read more easily.

Part 1 - Santa Solo Trip

# Day 3 - Part 1

from itertools import product

X = Y = 0
visited = [(0,0)] #(0,0) always visited
unique_houses_visited = 1 #house at (0,0)

with open('input.txt') as f:
  for c in f.read():
    if c == '^':
      Y += 1
    elif c == '<':
      X -= 1
    elif c == 'v':
      Y -= 1
    elif c == '>':
      X += 1
      
    # Check if new house
    if (X, Y) not in visited:
      # New house
      unique_houses_visited += 1
      visited.append((X, Y))

#answer      
print "Unique houses visited:", unique_houses_visited

Part 2 - Santa + Robo Santa

I didn't like the use of the santasTurn boolean here too much but couldn't think of a nicer way to complete the task (in the 15 minutes before I went to sleep!).

# Day 3 - Part 1

from itertools import product

santaX = santaY = 0
roboX = roboY = 0
visited = [(0,0)] #(0,0) always visited
unique_houses_visited = 1 #house at (0,0) visited by Santa and Robo-santa
santasTurn = True

def is_new_house(X, Y, visited):
  if (X, Y) not in visited:
    # New house
    visited.append((X, Y))
    return 1
  return 0

with open('input.txt') as f:
  for c in f.read():
    if c == '^':
      santaY += int(santasTurn)
      roboY += int(not santasTurn)
    elif c == '<':
      santaX -= int(santasTurn)
      roboX -= int(not santasTurn)
    elif c == 'v':
      santaY -= int(santasTurn)
      roboY -= int(not santasTurn)
    elif c == '>':
      santaX += int(santasTurn)
      roboX += int(not santasTurn)

    # Check if new house
    if santasTurn:
      unique_houses_visited += is_new_house(santaX, santaY, visited)
    else:
      unique_houses_visited += is_new_house(roboX, roboY, visited)
    
    # Swap turns!
    santasTurn = not santasTurn

#answer      
print "Unique houses visited:", unique_houses_visited

Leave a comment

(required)(will not be published)(required)

Comments

There are no comments yet. Be the first to add one!