t | def turtle(start, direction): | t | def turtle(coord, direction): |
| x, y = start | | x, y = coord |
| directions = [(1, 0), (0, 1), (-1, 0), (0, -1)] | | directions = [(1, 0), (0, 1), (-1, 0), (0, -1)] |
| while True: | | while True: |
| command = (yield (x, y)) | | command = (yield (x, y)) |
| if command == 'f': | | if command == 'f': |
| dx, dy = directions[direction] | | dx, dy = directions[direction] |
| x += dx | | x += dx |
| y += dy | | y += dy |
| elif command == 'l': | | elif command == 'l': |
| direction = (direction + 1) % 4 | | direction = (direction + 1) % 4 |
| elif command == 'r': | | elif command == 'r': |
| direction = (direction - 1) % 4 | | direction = (direction - 1) % 4 |