adventofcode/2017/03/1spiral_memory.py
2025-07-25 00:02:45 +00:00

42 lines
945 B
Python
Executable File

#!/usr/bin/env python3
if __name__ == '__main__':
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[0] += 1
else:
pointer[0] += -1
i += 1
xstep = inc_step(xstep)
i = 1
while i <= abs(ystep) and square < target:
square += 1
if ystep >= 0:
pointer[1] += 1
else:
pointer[1] += -1
i += 1
ystep = inc_step(ystep)
if square == target:
xloc = pointer[0]
yloc = pointer[1]
distance = abs(xloc) + abs(yloc)
print(f"dist to square {target} is {int(abs(xloc))}+{int(abs(yloc))}={distance}")