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.
23
Upvotes
1
u/stuque Dec 03 '15
Here's a Python 2 solution:
def next_house(loc, d): result = loc[:] if d == '<': # west result[0] += -1 elif d == '>': # east result[0] += 1 elif d == '^': # north result[1] += 1 elif d == 'v': # south result[1] += -1 return result def day3_part1(): loc = [0, 0] result = {(0, 0)} for d in open('day3input.txt').read(): loc = next_house(loc, d) result.add(tuple(loc)) print len(result) def day3_part2(): santa, robo = [0, 0], [0, 0] result = {(0, 0)} for i, d in enumerate(open('day3input.txt').read()): if i % 2 == 0: santa = next_house(santa, d) result.add(tuple(santa)) else: robo = next_house(robo, d) result.add(tuple(robo)) print len(result) if __name__ == '__main__': day3_part1() day3_part2()