-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
345 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"authors": [ | ||
"Corentin-Leffy" | ||
], | ||
"contributors": [ | ||
"Stargator", | ||
"kytrinyx" | ||
], | ||
"files": { | ||
"solution": [ | ||
"lib/robot_simulator.dart" | ||
], | ||
"test": [ | ||
"test/robot_simulator_test.dart" | ||
], | ||
"example": [ | ||
".meta/lib/example.dart" | ||
] | ||
}, | ||
"blurb": "Write a robot simulator.", | ||
"source": "Inspired by an interview question at a famous company." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"track":"dart","exercise":"robot-simulator","id":"2ea59c4a941c411793dac68608e35ee2","url":"https://exercism.org/tracks/dart/exercises/robot-simulator","handle":"LuCCoelho","is_requester":true,"auto_approve":false} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Help | ||
|
||
## Running the tests | ||
|
||
To run the tests: | ||
|
||
```sh | ||
$ dart test | ||
``` | ||
|
||
## Submitting your solution | ||
|
||
You can submit your solution using the `exercism submit lib/robot_simulator.dart` command. | ||
This command will upload your solution to the Exercism website and print the solution page's URL. | ||
|
||
It's possible to submit an incomplete solution which allows you to: | ||
|
||
- See how others have completed the exercise | ||
- Request help from a mentor | ||
|
||
## Need to get help? | ||
|
||
If you'd like help solving the exercise, check the following pages: | ||
|
||
- The [Dart track's documentation](https://exercism.org/docs/tracks/dart) | ||
- The [Dart track's programming category on the forum](https://forum.exercism.org/c/programming/dart) | ||
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5) | ||
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) | ||
|
||
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. | ||
|
||
To get help if you're having trouble, you can use one of the following resources: | ||
|
||
- [Dart API Documentation](https://api.dart.dev/) | ||
- [Dart Gitter Chat](https://gitter.im/dart-lang/home) | ||
- [Community Information](https://www.dart.dev/community) | ||
- [/r/dartlang](https://www.reddit.com/r/dartlang) is the Dart subreddit. | ||
- [StackOverflow](https://stackoverflow.com/questions/tagged/dart) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Robot Simulator | ||
|
||
Welcome to Robot Simulator on Exercism's Dart Track. | ||
If you need help running the tests or submitting your code, check out `HELP.md`. | ||
|
||
## Instructions | ||
|
||
Write a robot simulator. | ||
|
||
A robot factory's test facility needs a program to verify robot movements. | ||
|
||
The robots have three possible movements: | ||
|
||
- turn right | ||
- turn left | ||
- advance | ||
|
||
Robots are placed on a hypothetical infinite grid, facing a particular direction (north, east, south, or west) at a set of {x,y} coordinates, | ||
e.g., {3,8}, with coordinates increasing to the north and east. | ||
|
||
The robot then receives a number of instructions, at which point the testing facility verifies the robot's new position, and in which direction it is pointing. | ||
|
||
- The letter-string "RAALAL" means: | ||
- Turn right | ||
- Advance twice | ||
- Turn left | ||
- Advance once | ||
- Turn left yet again | ||
- Say a robot starts at {7, 3} facing north. | ||
Then running this stream of instructions should leave it at {9, 4} facing west. | ||
|
||
## Source | ||
|
||
### Created by | ||
|
||
- @Corentin-Leffy | ||
|
||
### Contributed to by | ||
|
||
- @Stargator | ||
- @kytrinyx | ||
|
||
### Based on | ||
|
||
Inspired by an interview question at a famous company. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
analyzer: | ||
errors: | ||
unused_element: error | ||
unused_import: error | ||
unused_local_variable: error | ||
dead_code: error | ||
|
||
linter: | ||
rules: | ||
# Error Rules | ||
- avoid_relative_lib_imports | ||
- avoid_types_as_parameter_names | ||
- literal_only_boolean_expressions | ||
- no_adjacent_strings_in_list | ||
- valid_regexps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
enum Orientation { north, east, south, west } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class Position { | ||
int x, y; | ||
|
||
Position(this.x, this.y); | ||
|
||
@override | ||
bool operator ==(Object other) => other is Position && other.x == this.x && other.y == this.y; | ||
|
||
@override | ||
int get hashCode { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + x; | ||
result = prime * result + y; | ||
return result; | ||
} | ||
|
||
@override | ||
String toString() => "[x: $x, y: $y]"; | ||
|
||
void advanceNorth() { | ||
this.y += 1; | ||
} | ||
|
||
void advanceEast() { | ||
this.x += 1; | ||
} | ||
|
||
void advanceSouth() { | ||
this.y -= 1; | ||
} | ||
|
||
void advanceWest() { | ||
this.x -= 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'package:robot_simulator/orientation.dart'; | ||
import 'package:robot_simulator/position.dart'; | ||
|
||
class Robot { | ||
Position position; | ||
Orientation orientation; | ||
|
||
Robot(this.position, this.orientation); | ||
|
||
void move(String instructions) { | ||
var instructionsList = instructions.split(''); | ||
List<Orientation> orientationsList = Orientation.values; | ||
for (String movement in instructionsList) { | ||
int orientationNum = orientationsList.indexOf(this.orientation); | ||
switch (movement) { | ||
case 'R': | ||
if (orientationNum >= 0 && orientationNum < 3) { | ||
int next = orientationNum + 1; | ||
this.orientation = orientationsList[next]; | ||
} else { | ||
this.orientation = orientationsList[0]; | ||
} | ||
continue; | ||
case 'L': | ||
if (orientationNum > 0 && orientationNum <= 3) { | ||
int previous = orientationNum - 1; | ||
this.orientation = orientationsList[previous]; | ||
} else { | ||
this.orientation = orientationsList[3]; | ||
} | ||
continue; | ||
default: | ||
switch (this.orientation) { | ||
case Orientation.north: | ||
this.position.advanceNorth(); | ||
continue; | ||
case Orientation.east: | ||
this.position.advanceEast(); | ||
continue; | ||
case Orientation.south: | ||
this.position.advanceSouth(); | ||
continue; | ||
default: | ||
this.position.advanceWest(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name: 'robot_simulator' | ||
environment: | ||
sdk: '>=2.18.0 <3.0.0' | ||
dev_dependencies: | ||
test: '<2.0.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import 'package:robot_simulator/orientation.dart'; | ||
import 'package:robot_simulator/position.dart'; | ||
import 'package:robot_simulator/robot_simulator.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void expected(Robot robot, {required Position beAt, required Orientation faceTo}) { | ||
expect(robot.position, equals(beAt)); | ||
expect(robot.orientation, equals(faceTo)); | ||
} | ||
|
||
void main() { | ||
group('RobotSimulator: Create robot - ', createRobot); | ||
group('RobotSimulator: Rotating clockwise - ', rotatingClockwise); | ||
group('RobotSimulator: Rotating counter-clockwise - ', rotatingCounterClockwise); | ||
group('RobotSimulator: Moving forward one - ', movingForwardOne); | ||
group('RobotSimulator: Follow series of instructions - ', followSeriesOfInstructions); | ||
} | ||
|
||
void createRobot() { | ||
test('at origin facing north', () { | ||
final robot = Robot(Position(0, 0), Orientation.north); | ||
expected(robot, beAt: Position(0, 0), faceTo: Orientation.north); | ||
}); | ||
|
||
test('at negative position facing south', () { | ||
final robot = Robot(Position(-1, -1), Orientation.south); | ||
expected(robot, beAt: Position(-1, -1), faceTo: Orientation.south); | ||
}); | ||
} | ||
|
||
void rotatingClockwise() { | ||
test('changes north to east', () { | ||
final robot = Robot(Position(0, 0), Orientation.north); | ||
robot.move('R'); | ||
expected(robot, beAt: Position(0, 0), faceTo: Orientation.east); | ||
}); | ||
|
||
test('changes east to south', () { | ||
final robot = Robot(Position(0, 0), Orientation.east); | ||
robot.move('R'); | ||
expected(robot, beAt: Position(0, 0), faceTo: Orientation.south); | ||
}); | ||
|
||
test('changes south to west', () { | ||
final robot = Robot(Position(0, 0), Orientation.south); | ||
robot.move('R'); | ||
expected(robot, beAt: Position(0, 0), faceTo: Orientation.west); | ||
}); | ||
|
||
test('changes west to north', () { | ||
final robot = Robot(Position(0, 0), Orientation.west); | ||
robot.move('R'); | ||
expected(robot, beAt: Position(0, 0), faceTo: Orientation.north); | ||
}); | ||
} | ||
|
||
void rotatingCounterClockwise() { | ||
test('changes north to west', () { | ||
final robot = Robot(Position(0, 0), Orientation.north); | ||
robot.move('L'); | ||
expected(robot, beAt: Position(0, 0), faceTo: Orientation.west); | ||
}); | ||
|
||
test('changes west to south', () { | ||
final robot = Robot(Position(0, 0), Orientation.west); | ||
robot.move('L'); | ||
expected(robot, beAt: Position(0, 0), faceTo: Orientation.south); | ||
}); | ||
|
||
test('changes south to east', () { | ||
final robot = Robot(Position(0, 0), Orientation.south); | ||
robot.move('L'); | ||
expected(robot, beAt: Position(0, 0), faceTo: Orientation.east); | ||
}); | ||
|
||
test('changes east to north', () { | ||
final robot = Robot(Position(0, 0), Orientation.east); | ||
robot.move('L'); | ||
expected(robot, beAt: Position(0, 0), faceTo: Orientation.north); | ||
}); | ||
} | ||
|
||
void movingForwardOne() { | ||
test('facing north increments Y', () { | ||
final robot = Robot(Position(0, 0), Orientation.north); | ||
robot.move('A'); | ||
expected(robot, beAt: Position(0, 1), faceTo: Orientation.north); | ||
}); | ||
|
||
test('facing south decrements Y', () { | ||
final robot = Robot(Position(0, 0), Orientation.south); | ||
robot.move('A'); | ||
expected(robot, beAt: Position(0, -1), faceTo: Orientation.south); | ||
}); | ||
|
||
test('facing east increments X', () { | ||
final robot = Robot(Position(0, 0), Orientation.east); | ||
robot.move('A'); | ||
expected(robot, beAt: Position(1, 0), faceTo: Orientation.east); | ||
}); | ||
|
||
test('facing west decrements X', () { | ||
final robot = Robot(Position(0, 0), Orientation.west); | ||
robot.move('A'); | ||
expected(robot, beAt: Position(-1, 0), faceTo: Orientation.west); | ||
}); | ||
} | ||
|
||
void followSeriesOfInstructions() { | ||
test('moving east and north from README', () { | ||
final robot = Robot(Position(7, 3), Orientation.north); | ||
robot.move('RAALAL'); | ||
expected(robot, beAt: Position(9, 4), faceTo: Orientation.west); | ||
}); | ||
|
||
test('moving west and north', () { | ||
final robot = Robot(Position(0, 0), Orientation.north); | ||
robot.move('LAAARALA'); | ||
expected(robot, beAt: Position(-4, 1), faceTo: Orientation.west); | ||
}); | ||
|
||
test('moving west and south', () { | ||
final robot = Robot(Position(2, -7), Orientation.east); | ||
robot.move('RRAAAAALA'); | ||
expected(robot, beAt: Position(-3, -8), faceTo: Orientation.south); | ||
}); | ||
|
||
test('moving east and north', () { | ||
final robot = Robot(Position(8, 4), Orientation.south); | ||
robot.move('LAAARRRALLLL'); | ||
expected(robot, beAt: Position(11, 5), faceTo: Orientation.north); | ||
}); | ||
} |