n | def turtle(start_position, start_direction): | n | def turtle(position, initial_direction): |
| current_x, current_y = start_position | | pos_x, pos_y = position |
| direction_steps = [(1, 0), (0, 1), (-1, 0), (0, -1)] | | moves = [(1, 0), (0, 1), (-1, 0), (0, -1)] |
| current_direction = start_direction | | facing = initial_direction |
| while True: | | while True: |
n | command = (yield (current_x, current_y)) | n | command = (yield (pos_x, pos_y)) |
| if command == 'f': | | if command == 'f': |
n | dx, dy = direction_steps[current_direction] | n | move_x, move_y = moves[facing] |
| current_x += dx | | pos_x += move_x |
| current_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 |