enum Direction { north('N'), east('E'), south('S'), west('W'); final String code; const Direction(this.code); static Direction fromCode(String code) { return Direction.values.firstWhere((d) => d.code == code); } Direction turnLeft() { return Direction.values[(index + 3) % 4]; } Direction turnRight() { return Direction.values[(index + 1) % 4]; } } class Rover { int x; int y; Direction _direction; final int plateauWidth; final int plateauHeight; Rover({ required this.x, required this.y, required String direction, this.plateauWidth = 100, this.plateauHeight = 100, }) : _direction = Direction.fromCode(direction); String get direction => _direction.code; void turnLeft() { _direction = _direction.turnLeft(); } void turnRight() { _direction = _direction.turnRight(); } void moveForward() { switch (_direction) { case Direction.north: y = _wrapCoordinate(y + 1, plateauHeight); break; case Direction.east: x = _wrapCoordinate(x + 1, plateauWidth); break; case Direction.south: y = _wrapCoordinate(y - 1, plateauHeight); break; case Direction.west: x = _wrapCoordinate(x - 1, plateauWidth); break; } } int _wrapCoordinate(int value, int max) { final wrapped = value % (max + 1); return wrapped < 0 ? wrapped + max + 1 : wrapped; } void execute(String commands) { for (var i = 0; i < commands.length; i++) { final command = commands[i]; switch (command) { case 'L': turnLeft(); break; case 'R': turnRight(); break; case 'M': moveForward(); break; } } } }