n | def turtle(coord, direction): | n | def turtle(position, initial_direction): |
| x, y = coord | | pos_x, pos_y = position |
| directions = [(1, 0), (0, 1), (-1, 0), (0, -1)] | | moves = [(1, 0), (0, 1), (-1, 0), (0, -1)] |
| current_direction = direction | | facing = initial_direction |
| while True: | | while True: |
n | command = (yield (x, y)) | n | command = (yield (pos_x, pos_y)) |
| if command == 'f': | | if command == 'f': |
n | dx, dy = directions[current_direction] | n | move_x, move_y = moves[facing] |
| x += dx | | pos_x += move_x |
| y += dy | | pos_y += move_y |
| elif command == 'l': | | elif command == 'l': |
n | current_direction = (current_direction + 1) % 4 | n | facing = (facing + 1) % 4 |
| elif command == 'r': | | elif command == 'r': |
t | current_direction = (current_direction - 1) % 4 | t | facing = (facing - 1) % 4 |