r/adventofcode • • 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

228 comments sorted by

View all comments

Show parent comment

1

u/eclair4151 Dec 03 '15

for future reference if you just have some ints like an x and y you don't need to make a whole class, just use a tuple (x,y). I wrote mine in python and its a bit cleaner

f = open('file.txt','r+')
santaMoving = True

santax = 0
santay = 0

robox = 0
roboy = 0

locs = {(0,0)}


for c in f.read():
    if santaMoving:
        if c is '^':
            santay-=1
        elif c is 'v':
            santay+=1
        elif c is '<':
            santax-=1
        elif c is '>':
            santax+=1
        locs.add((santax,santay))
    else:
        if c is '^':
            roboy-=1
        elif c is 'v':
            roboy+=1
        elif c is '<':
            robox-=1
        elif c is '>':
            robox+=1
        locs.add((robox,roboy))
    santaMoving = not santaMoving

print(len(locs))

2

u/piratedicecream Dec 03 '15 edited Dec 03 '15

Thanks for the note! The reason i made the class was cause in the heat of the moment that was the way I was thinking about it.

EDIT: I might argue cleanliness in favor of the class because of the consolidated move methods.

EDIT 2: If i wasn't keeping track of the count of each coordinate, the for statement would be hella clean

for arrow in directions :
    if switch :
        santa.move(arrow)
        coord_list.add(santa.x,santa.y)
    else :
        robot.move(arrow)
        coord_list.add(santa.x,santa.y)

     switch = not switch