Skip to content

Commit

Permalink
Add robot-simulator exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras committed Jan 17, 2024
1 parent 45fa165 commit 11412bc
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "robot-simulator",
"name": "Robot Simulator",
"uuid": "31a8cb4a-c211-4daa-a14b-7347fed015a6",
"practices": [],
"prerequisites": [],
"difficulty": 4
}
]
},
Expand Down
25 changes: 25 additions & 0 deletions exercises/practice/robot-simulator/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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.
18 changes: 18 additions & 0 deletions exercises/practice/robot-simulator/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"robot_simulator.gd"
],
"test": [
"robot_simulator_test.gd"
],
"example": [
".meta/example.gd"
]
},
"blurb": "Write a robot simulator.",
"source": "Inspired by an interview question at a famous company."
}
27 changes: 27 additions & 0 deletions exercises/practice/robot-simulator/.meta/example.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var current_position: Vector2i
var current_direction: String
var allowed_directions = ['north', 'east', 'south', 'west']

func _init(position: Vector2i = Vector2i(0, 0), direction: String = 'north'):
current_position = position
current_direction = direction


func move(instructions: String):
for instruction in instructions:
match [instruction, current_direction]:
['L', _]:
var index = allowed_directions.find(current_direction) - 1
current_direction = allowed_directions[index]
['R', _]:
var index = allowed_directions.find(current_direction)
index = (index + 1) % 4
current_direction = allowed_directions[index]
['A', 'north']:
current_position += Vector2i(0, 1)
['A', 'south']:
current_position += Vector2i(0, -1)
['A', 'east']:
current_position += Vector2i(1, 0)
['A', 'west']:
current_position += Vector2i(-1, 0)
64 changes: 64 additions & 0 deletions exercises/practice/robot-simulator/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[c557c16d-26c1-4e06-827c-f6602cd0785c]
description = "Create robot -> at origin facing north"

[bf0dffce-f11c-4cdb-8a5e-2c89d8a5a67d]
description = "Create robot -> at negative position facing south"

[8cbd0086-6392-4680-b9b9-73cf491e67e5]
description = "Rotating clockwise -> changes north to east"

[8abc87fc-eab2-4276-93b7-9c009e866ba1]
description = "Rotating clockwise -> changes east to south"

[3cfe1b85-bbf2-4bae-b54d-d73e7e93617a]
description = "Rotating clockwise -> changes south to west"

[5ea9fb99-3f2c-47bd-86f7-46b7d8c3c716]
description = "Rotating clockwise -> changes west to north"

[fa0c40f5-6ba3-443d-a4b3-58cbd6cb8d63]
description = "Rotating counter-clockwise -> changes north to west"

[da33d734-831f-445c-9907-d66d7d2a92e2]
description = "Rotating counter-clockwise -> changes west to south"

[bd1ca4b9-4548-45f4-b32e-900fc7c19389]
description = "Rotating counter-clockwise -> changes south to east"

[2de27b67-a25c-4b59-9883-bc03b1b55bba]
description = "Rotating counter-clockwise -> changes east to north"

[f0dc2388-cddc-4f83-9bed-bcf46b8fc7b8]
description = "Moving forward one -> facing north increments Y"

[2786cf80-5bbf-44b0-9503-a89a9c5789da]
description = "Moving forward one -> facing south decrements Y"

[84bf3c8c-241f-434d-883d-69817dbd6a48]
description = "Moving forward one -> facing east increments X"

[bb69c4a7-3bbf-4f64-b415-666fa72d7b04]
description = "Moving forward one -> facing west decrements X"

[e34ac672-4ed4-4be3-a0b8-d9af259cbaa1]
description = "Follow series of instructions -> moving east and north from README"

[f30e4955-4b47-4aa3-8b39-ae98cfbd515b]
description = "Follow series of instructions -> moving west and north"

[3e466bf6-20ab-4d79-8b51-264165182fca]
description = "Follow series of instructions -> moving west and south"

[41f0bb96-c617-4e6b-acff-a4b279d44514]
description = "Follow series of instructions -> moving east and north"
6 changes: 6 additions & 0 deletions exercises/practice/robot-simulator/robot_simulator.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
func _init(position, direction):
pass


func move(instructions):
pass
126 changes: 126 additions & 0 deletions exercises/practice/robot-simulator/robot_simulator_test.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
func same_field_values(robot_a, robot_b):
return robot_a.position == robot_b.position && robot_a.direction == robot_b.direction


func test_create_robot_at_origin_facing_north(solution_script):
var robot_a = solution_script.new(Vector2i(0, 0), 'north')
var robot_b = solution_script.new(Vector2i(0, 0), 'north')
return [true, same_field_values(robot_a, robot_b)]


func test_create_robot_at_negative_position_facing_south(solution_script):
var robot_a = solution_script.new(Vector2i(-1, -1), 'south')
var robot_b = solution_script.new(Vector2i(-1, -1), 'south')
return [true, same_field_values(robot_a, robot_b)]


func test_rotating_clockwise_changes_north_to_east(solution_script):
var robot_a = solution_script.new(Vector2i(0, 0), 'north')
robot_a.move('R')
var robot_b = solution_script.new(Vector2i(0, 0), 'east')
return [true, same_field_values(robot_a, robot_b)]


func test_rotating_clockwise_changes_east_to_south(solution_script):
var robot_a = solution_script.new(Vector2i(0, 0), 'east')
robot_a.move('R')
var robot_b = solution_script.new(Vector2i(0, 0), 'south')
return [true, same_field_values(robot_a, robot_b)]


func test_rotating_clockwise_changes_south_to_west(solution_script):
var robot_a = solution_script.new(Vector2i(0, 0), 'south')
robot_a.move('R')
var robot_b = solution_script.new(Vector2i(0, 0), 'west')
return [true, same_field_values(robot_a, robot_b)]


func test_rotating_clockwise_changes_west_to_north(solution_script):
var robot_a = solution_script.new(Vector2i(0, 0), 'west')
robot_a.move('R')
var robot_b = solution_script.new(Vector2i(0, 0), 'north')
return [true, same_field_values(robot_a, robot_b)]


func test_rotating_counterclockwise_changes_north_to_west(solution_script):
var robot_a = solution_script.new(Vector2i(0, 0), 'north')
robot_a.move('L')
var robot_b = solution_script.new(Vector2i(0, 0), 'west')
return [true, same_field_values(robot_a, robot_b)]


func test_rotating_counterclockwise_changes_west_to_south(solution_script):
var robot_a = solution_script.new(Vector2i(0, 0), 'west')
robot_a.move('L')
var robot_b = solution_script.new(Vector2i(0, 0), 'south')
return [true, same_field_values(robot_a, robot_b)]


func test_rotating_counterclockwise_changes_south_to_east(solution_script):
var robot_a = solution_script.new(Vector2i(0, 0), 'south')
robot_a.move('L')
var robot_b = solution_script.new(Vector2i(0, 0), 'east')
return [true, same_field_values(robot_a, robot_b)]


func test_rotating_counterclockwise_changes_east_to_north(solution_script):
var robot_a = solution_script.new(Vector2i(0, 0), 'east')
robot_a.move('L')
var robot_b = solution_script.new(Vector2i(0, 0), 'north')
return [true, same_field_values(robot_a, robot_b)]


func test_moving_forward_one_facing_north_increments_y(solution_script):
var robot_a = solution_script.new(Vector2i(0, 0), 'north')
robot_a.move('A')
var robot_b = solution_script.new(Vector2i(0, 1), 'north')
return [true, same_field_values(robot_a, robot_b)]


func test_moving_forward_one_facing_south_decrements_y(solution_script):
var robot_a = solution_script.new(Vector2i(0, 0), 'south')
robot_a.move('A')
var robot_b = solution_script.new(Vector2i(0, -1), 'south')
return [true, same_field_values(robot_a, robot_b)]


func test_moving_forward_one_facing_east_increments_x(solution_script):
var robot_a = solution_script.new(Vector2i(0, 0), 'east')
robot_a.move('A')
var robot_b = solution_script.new(Vector2i(1, 0), 'east')
return [true, same_field_values(robot_a, robot_b)]


func test_moving_forward_one_facing_west_decrements_x(solution_script):
var robot_a = solution_script.new(Vector2i(0, 0), 'west')
robot_a.move('A')
var robot_b = solution_script.new(Vector2i(-1, 0), 'west')
return [true, same_field_values(robot_a, robot_b)]


func test_moving_east_and_north_from_readme(solution_script):
var robot_a = solution_script.new(Vector2i(7, 3), 'north')
robot_a.move('RAALAL')
var robot_b = solution_script.new(Vector2i(9, 4), 'west')
return [true, same_field_values(robot_a, robot_b)]


func test_moving_west_and_north(solution_script):
var robot_a = solution_script.new(Vector2i(0, 0), 'north')
robot_a.move('LAAARALA')
var robot_b = solution_script.new(Vector2i(-4, 1), 'west')
return [true, same_field_values(robot_a, robot_b)]


func test_moving_west_and_south(solution_script):
var robot_a = solution_script.new(Vector2i(2, -7), 'east')
robot_a.move('RRAAAAALA')
var robot_b = solution_script.new(Vector2i(-3, 8), 'south')
return [true, same_field_values(robot_a, robot_b)]


func test_moving_west_and_south(solution_script):
var robot_a = solution_script.new(Vector2i(8, 4), 'south')
robot_a.move('LAAARRRALLLL')
var robot_b = solution_script.new(Vector2i(11, 5), 'north')
return [true, same_field_values(robot_a, robot_b)]

0 comments on commit 11412bc

Please sign in to comment.