2025-07-25 00:02:45 +00:00

63 lines
1.4 KiB
Plaintext

spiral memory!
i think i'll keep a live count of the current position relative to square 1,
slowly stepping one by one through each square sequentially.
my input is only 277678, so this should be fine!
i notice that the pointer starts at square 1, at, say, (0,0),
and then moves one square right, then one up,
then two left, then two down,
then three right, three up, etc
seems like it follows the pattern:
(x1 , y1 ) = (0 , 0 )
(x2 , y2 ) = (x1 + 1 , y1 )
(x3 , y3 ) = (x2 , y1 + 1)
(x5 , y5 ) = (x3 - 2 , y3 )
(x7 , y7 ) = (x5 , y3 - 2)
(x10, y10) = (x5 + 3 , y7 )
etc.
i'll just for loop this shit
something like: (copied to 1spiral_memory.py and worked further from there)
(the following is my first rough sketch. see 1spiral_memory.py)
target = 277678
xstep = 1
ystep = 1
origin = (0, 0)
square = 1
pointer = origin
def inc_step(step):
if step >= 0:
return int(-1 * (step + 1))
else:
return int(-1 * (step - 1))
while square < target:
i = 1
while i <= abs(xstep) and square < target:
square += 1
if xstep >= 0:
pointer += (1, 0)
else:
pointer -= (1, 0)
i += 1
xstep = inc_step(xstep)
i = 1
while i <= abs(ystep) and square < target:
square += 1
if ystep >= 0:
pointer += (0, 1)
else:
pointer -= (0, 1)
ystep = inc_step(ystep)
if square == target:
xloc = pointer[0]
yloc = pointer[1]
distance = abs(xloc) + abs(yloc)