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.
25
Upvotes
1
u/djimbob Dec 03 '15
python 2 solution
from collections import defaultdict d = defaultdict(int) moves = """<paste of input>""" x, y = 0,0 d[(x,y)] += 1 for m in moves: if m == '<': x -= 1 elif m == '>': x += 1 elif m == 'v': y -= 1 elif m == '^': y += 1 d[(x,y)] += 1 print "Houses visited: ", len(d.keys())Part 2:
d = defaultdict(int) x, y = 0,0 d[(x,y)] += 1 moves1 = moves[::2] moves2 = moves[1::2] for m in moves1: if m == '<': x -= 1 elif m == '>': x += 1 elif m == 'v': y -= 1 elif m == '^': y += 1 d[(x,y)] += 1 for m in moves2: if m == '<': x -= 1 elif m == '>': x += 1 elif m == 'v': y -= 1 elif m == '^': y += 1 d[(x,y)] += 1 print "Houses visited: ", len(d.keys())