Skip to content

Instantly share code, notes, and snippets.

@PurpleMyst
Created December 9, 2017 23:48
Show Gist options
  • Save PurpleMyst/d6e6db4ee6cf23b5ee7334eb0a48caca to your computer and use it in GitHub Desktop.
Save PurpleMyst/d6e6db4ee6cf23b5ee7334eb0a48caca to your computer and use it in GitHub Desktop.
Solution to AoC 2017 Day 3 Part 1
#!/usr/bin/env python3
UP = (0, -1)
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)
NEXT_DIRECTION = {
RIGHT: UP,
UP: LEFT,
LEFT: DOWN,
DOWN: RIGHT
}
def spiral_position(target):
assert target >= 1
x, y = 0, 0
value = 1
magnitude = 1
direction = RIGHT
while True:
for _ in range(int(magnitude)):
if value == target:
return (x, y)
x += direction[0]
y += direction[1]
value += 1
direction = NEXT_DIRECTION[direction]
magnitude += 0.5
def spiral_distance(target):
x, y = spiral_position(target)
return abs(x) + abs(y)
def main():
with open("input.txt") as input_file:
target = int(input_file.read())
print(spiral_distance(target))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment