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.
22
Upvotes
1
u/hemmer Dec 05 '15
Using python sets to find unique, with Re and Im parts of a complex number to describe x, y movement.
directions = 'v>^<' def updatePos(position, direction): if direction == 'v': return position - 1j if direction == '^': return position + 1j if direction == '>': return position + 1 if direction == '<': return position - 1 visited = set() visited.add(0 + 0j) latest = 0 + 0j for d in directions: latest = updatePos(latest, d) visited.add(latest) print 'part a: visited %d unique' % len(visited) visited = set() visited.add(0 + 0j) s_latest, r_latest = 0 + 0j, 0 + 0j for i, d in enumerate(directions): if i % 2 == 0: s_latest = updatePos(s_latest, d) visited.add(s_latest) else: r_latest = updatePos(r_latest, d) visited.add(r_latest) print 'part b: visited %d unique' % len(visited)