n | def turtle(position, heading): | n | def turtle(coord, direction): |
| turn_offsets = {0: (1, 0), 1: (0, 1), 2: (-1, 0), 3: (0, -1)} | | directions = {0: (1, 0), 1: (0, 1), 2: (-1, 0), 3: (0, -1)} |
| x, y = position | | x, y = coord |
| while True: | | while True: |
t | instruction = (yield (x, y)) | t | command = (yield (x, y)) |
| if instruction == 'f': | | if command == 'f': |
| offset_x, offset_y = turn_offsets[heading] | | dx, dy = directions[direction] |
| x += offset_x | | x += dx |
| y += offset_y | | y += dy |
| elif instruction == 'l': | | elif command == 'l': |
| heading = (heading + 1) % 4 | | direction = (direction + 1) % 4 |
| elif instruction == 'r': | | elif command == 'r': |
| heading = (heading - 1) % 4 | | direction = (direction - 1) % 4 |