Skip to content

Instantly share code, notes, and snippets.

@pawlos
Last active December 24, 2017 18:52
Show Gist options
  • Save pawlos/b3bdcbb781e0fc1d3bd887d414583863 to your computer and use it in GitHub Desktop.
Save pawlos/b3bdcbb781e0fc1d3bd887d414583863 to your computer and use it in GitHub Desktop.
Solution to Day 3: Spiral Memory - Part 2
#aoc_32.py
'''
147 142 133 122 59
304 5 4 2 57
330 10 1 1 54
351 11 23 25 26
362 747 806---> ...
1 1 2 4 5 10 11 23 25 26 54 57 59 122 133 142 147 304 330 351 362 747 806 880 931
'''
#ans: 752688 <- wrong
def sum(l,i):
s3 = 0
s1 = 0
#print l,i
if l != len(data)-1:
s3 = data[l+1][i] + (data[l+1][i+1] if len(data)-1 != i else 0) + (data[l+1][i-1] if i != 0 else 0)
if l != 0:
s1 = data[l-1][i] + (data[l-1][i+1] if len(data)-1 != i else 0) + (data[l-1][i-1] if i != 0 else 0)
v = s3 + s1 + (data[l][i+1] if len(data)-1!=i else 0) + (data[l][i-1] if i !=0 else 0)
#print v
return v
n = 10
data = []*n
for i in range(n):
data.append([0]*n)
#print len(data)
def solve(inp):
l = len(data)/2
i = len(data)/2
#print "l: ",l, "i:",i
data[l][i] = 1
delta_x = 1
delta_y = 0
s = 1
cnt = 1
for k in range(15):
#print "d_x: ", delta_x, "d_y: ", delta_y, "cnt: ",cnt
for w in range(int(cnt)):
l += delta_y
i += delta_x
if i == len(data):
print "too_small"
return
data[l][i] = sum(l,i)
if data[l][i] > inp:
print data
return data[l][i]
#print "change"
if delta_x == 1:
delta_x = 0
delta_y = -1
elif delta_y == -1:
delta_x = -1
delta_y = 0
elif delta_x == -1:
delta_x = 0
delta_y = 1
elif delta_y == 1:
delta_x = 1
delta_y = 0
cnt += 0.5
print data
print ""
print "368078: "
print solve(368078)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment