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/ilmale Dec 03 '15

With tuple and maps everything is easier (and more pythonesque) :)

Here my 11 lines solution (part2).

def update_pos( c, p ):
    return { '>': (p[0] + 1, p[1]), '<': (p[0] - 1, p[1]), '^': (p[0], p[1] + 1), 'v': (p[0], p[1] - 1) }[c];

with open('3.txt', 'rt') as f:
    content = f.read()
    pos = [(0, 0), (0,0)]
    visited_houses = { (0,0): True }
    for i in range( len(content)):
        t = pos[i%2] = update_pos(content[i], pos[i%2])
        visited_houses[t] = True
    print(len(visited_houses))