Skip to content

Instantly share code, notes, and snippets.

@pawlos
Last active December 24, 2017 18:51
Show Gist options
  • Save pawlos/0cefa9d753bd6416e6cc9a456ed787f7 to your computer and use it in GitHub Desktop.
Save pawlos/0cefa9d753bd6416e6cc9a456ed787f7 to your computer and use it in GitHub Desktop.
Solution to Day 3: Spiral Memory
#aoc_3.py
'''
17 16 15 14 13
18 5 4 3 12
19 6 1 2 11
20 7 8 9 10
21 22 23---> ...
'''
lines = open('out.txt','r').readlines()
data = [[int(d) for d in l.replace(' \n','').split(' ')] for l in lines]
#print len(data)
def solve(inp):
found_l = 0
found_i = 0
found_i1 = 0
found_l1 = 0
for l in range(len(data)):
for i in range(len(data[l])):
if data[l][i] == inp:
found_i = i
found_l = l
break
if data[l][i] == 1:
found_i1 = i
found_l1 = l
# found
print "found_i:", found_i, "found_l: ",found_l
print "found_i1:", found_i1, "found_l1: ", found_l1
add_1 = False#((found_l == found_l1) or (found_i1 == found_i)) == False
print "Add 1?", add_1
return (abs(found_i1-found_i))+abs(found_l1 - found_l) + (1 if add_1 else 0)
print "12: "
print solve(12)
print "---------"
print "23: "
print solve(23)
print "---------"
print "1024: "
print solve(1024)
print "---------"
print "368078: "
print solve(368078)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment