-
-
Save manlon/143cdc505778aeacd0c1f0de1f94baec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import cycle, islice | |
def coords(): | |
dirs = cycle([(1,0), (0,1), (-1,0), (0, -1)]) | |
(dx, dy) = next(dirs) | |
yield (0,0) | |
minx = miny = maxx = maxy = 0 | |
x = y = 0 | |
while True: | |
(x,y) = (x + dx, y + dy) | |
yield (x,y) | |
if x > maxx or x < minx or y > maxy or y < miny: | |
maxx, maxy, minx, miny = (max(x, maxx), max(y,maxy), min(x, minx), min(y, miny)) | |
(dx, dy) = next(dirs) | |
def sums(): | |
board = {(0,0): 1} | |
for (x,y) in coords(): | |
neighbors = (((x+dx), (y+dy)) for dx in xrange(-1,2) for dy in xrange(-1,2)) | |
val = sum(board.get(n,0) for n in neighbors) | |
yield val | |
board[(x,y)] = val | |
target = 277678 | |
#part1 | |
coord = islice(coords(), target, target+1) | |
print list(abs(x)+abs(y) for (x,y) in coord)[0] | |
#part2 | |
for x in sums(): | |
if x > target: | |
print x | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment