Skip to content

Instantly share code, notes, and snippets.

@gnyman
Created December 9, 2022 19:00
Embed
What would you like to do?
# define the initial grid and positions of head and tail
grid = ['......', '......', '......', '......', 'H.....']
head = (0, 4)
tail = (0, 4)
# simulate the first movement (R 4)
head = (head[0], head[1] + 4)
# update the tail position if necessary
if not is_adjacent(head, tail):
if head[0] == tail[0]:
tail = (tail[0], tail[1] + 1 if head[1] > tail[1] else tail[1] - 1)
elif head[1] == tail[1]:
tail = (tail[0] + 1 if head[0] > tail[0] else tail[0] - 1, tail[1])
else:
tail = (tail[0] + 1 if head[0] > tail[0] else tail[0] - 1, tail[1] + 1 if head[1] > tail[1] else tail[1] - 1)
# update the grid
grid[head[0]] = grid[head[0]][:head[1]] + 'H' + grid[head[0]][head[1] + 1:]
grid[tail[0]] = grid[tail[0]][:tail[1]] + 'T' + grid[tail[0]][tail[1] + 1:]
# print the resulting grid
for row in grid:
print(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment