Skip to content

Instantly share code, notes, and snippets.

@gnyman
Created December 10, 2022 12:05
Show Gist options
  • Save gnyman/05607764eeda3d018143154f787ec8fc to your computer and use it in GitHub Desktop.
Save gnyman/05607764eeda3d018143154f787ec8fc to your computer and use it in GitHub Desktop.
# initialize the head and tail at the same position
head = (0, 0)
tail = (0, 0)
# define the list of movements for the head
movements = ["R 4", "U 4", "L 3", "D 1", "R 4", "D 1", "L 5", "R 2"]
# iterate over the movements and update the position of the head and tail
for move in movements:
direction, steps = move[0], int(move[1])
if direction == "R":
head = (head[0] + steps, head[1])
elif direction == "L":
head = (head[0] - steps, head[1])
elif direction == "U":
head = (head[0], head[1] + steps)
elif direction == "D":
head = (head[0], head[1] - steps)
# check if the tail is still adjacent to the head, and move it if necessary
if abs(head[0] - tail[0]) > 1 or abs(head[1] - tail[1]) > 1:
tail = (head[0] - steps, head[1])
# print the final positions of the head and tail
print("Final position of the head:", head)
print("Final position of the tail:", tail)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment