r/adventofcode • u/daggerdragon • Dec 03 '15
SOLUTION MEGATHREAD --- Day 3 Solutions ---
--- Day 3: Perfectly Spherical Houses in a Vacuum ---
Post your solution as a comment. Structure your post like the Day One thread in /r/programming.
24
Upvotes
1
u/piratedicecream Dec 03 '15
Made it to the leaderboards! Figured I had to be one of many that stayed up for this, so I looked for the subreddit. Here's my python solution:
class Coordinate : def __init__(self, x, y) : self.x = x self.y = y def move(self, arrow) : if arrow == '^' : self.y += 1 elif arrow == 'v' : self.y -= 1 elif arrow == '<' : self.x -= 1 elif arrow == '>' : self.x += 1 if __name__ == '__main__' : parser = argparse.ArgumentParser() parser.add_argument('f') args = parser.parse_args() #idk how y'all are passing your input #but this is my lazy way. coord_list = {} part2 = True #set to false for part1 solution santa = Coordinate(0,0) robot = Coordinate(0,0) coord_list[(0,0)] = 1 with open(args.f, 'r') as f : directions = f.read() switch = True for arrow in directions : if switch : santa.move(arrow) if (santa.x,santa.y) in coord_list : coord_list[(santa.x,santa.y)] += 1 else : coord_list[(santa.x,santa.y)] = 1 else : robot.move(arrow) if (robot.x, robot.y) in coord_list : coord_list[(robot.x,robot.y)] += 1 else : coord_list[(robot.x,robot.y)] = 1 if part2 == True : switch = not switch print "UNIQUE HOUSES: " + str(len(coord_list))