Skip to content

Instantly share code, notes, and snippets.

@Mark-McCracken
Created September 23, 2022 10:58
Show Gist options
  • Save Mark-McCracken/28f16ad5d6a0a5a96c3c9ce17f37fdcc to your computer and use it in GitHub Desktop.
Save Mark-McCracken/28f16ad5d6a0a5a96c3c9ce17f37fdcc to your computer and use it in GitHub Desktop.
Robot Warehouse code
import { myConst, Robot, Direction } from ".";
describe("Pairing test", () => {
it("runs", () => {
expect(myConst).toBe(true);
});
});
describe("basic robot movement", () => {
it("moves in each direction successfully", () => {
const startX = 1;
const startY = 3;
const myRobot = new Robot({x: startX, y: startY});
myRobot.move(Direction.North);
expect(myRobot.position.y).toBe(startY+1);
myRobot.move(Direction.East);
expect(myRobot.position.x).toBe(startX+1);
myRobot.move(Direction.South);
expect(myRobot.position.y).toBe(startY);
myRobot.move(Direction.West);
expect(myRobot.position.x).toBe(startX);
});
it("should not move outside the bounds of the warehouse", () => {
const myRobot = new Robot();
expect(myRobot.position.x).toBe(0);
expect(myRobot.position.y).toBe(0);
myRobot.move(Direction.South);
expect(myRobot.position.y).toBe(0);
myRobot.move(Direction.West);
expect(myRobot.position.x).toBe(0);
});
});
describe("sequencing movements", () => {
it("should make multiple moves", () => {
const myRobot = new Robot();
myRobot.moveSequence("N E N E N E N E");
expect(myRobot.position.x).toBe(4);
expect(myRobot.position.y).toBe(4);
});
it("should not go outside multiple moves", () => {
const myRobot = new Robot();
myRobot.moveSequence("N E S S");
expect(myRobot.position.x).toBe(1);
expect(myRobot.position.y).toBe(0);
});
it("should ignore nonsense commands", () => {
const myRobot = new Robot();
myRobot.moveSequence("N E N E A B C");
expect(myRobot.position.x).toBe(2);
expect(myRobot.position.y).toBe(2);
});
});
describe("crateMovement", () => {
it("should pickup a crate and move it", () => {
const myRobot = new Robot();
const positions = Array(10).fill(Array(10).fill(false));
positions[9][9] = true;
positions[4][4] = true;
//
});
});
const myConst = true;
export { myConst, Robot, Direction, Warehouse };
interface Position {
x: number
y: number
}
enum Direction {
North = "N",
South = "S",
East = "E",
West = "W"
}
const allEnumValues: string[] = [Direction.North, Direction.East, Direction.South, Direction.West];
// grid: 0-9 on x axis, where 0 is more west and 9 is more east
// 0-9 on y axis, where 0 is more south, and 9 is more north.
class Robot {
position: Position = {
x: 0,
y: 0
};
isHoldingACrate: boolean = false;
constructor(position: Position = {x: 0, y: 0}) {
this.position = position;
}
move(direction: Direction) {
switch (direction) {
case Direction.North:
if (this.position.y >= 9) break;
this.position.y += 1;
break;
case Direction.South:
if (this.position.y <= 0) break;
this.position.y -= 1;
break;
case Direction.East:
if (this.position.x >= 9) break;
this.position.x += 1;
break
case Direction.West:
if (this.position.x <= 0) break;
this.position.x -= 1;
break;
}
}
pickup(warehouse: Warehouse) {
// check if crate exists at current position;
// otherwise, log a message to say it can't find a crate
this.isHoldingACrate = true;
// warehouse.removeCrateFromPosition(this.position);
}
setDown() {
// check there's nothign already sitting here
this.isHoldingACrate = false;
// warehouse.setCrateAtPosition(this.position)
}
moveSequence(commands: string) {
console.log(`Moving from Position x: ${this.position.x}, y: ${this.position.y}`);
const steps: string[] = commands.split(" ");
const validSteps: Direction[] = steps.filter((i) => {
const isValid = allEnumValues.includes(i);
if (isValid) return true;
console.log(`Got a value in the sequence of commands that does not adhere to a valid direction.
Got: ${i}.
Expected one of : ${allEnumValues.toString()}
`);
return false;
}) as Direction[];
validSteps.forEach(step => {
this.move(step);
});
console.log(`New location after moving through sequence: x: ${this.position.x}, y: ${this.position.y}`)
}
}
class Warehouse {
creates: Array<Array<boolean>>;
constructor(currentcrates: Array<Array<boolean>> = Array(10).fill(Array(10).fill(false))) {
this.creates = currentcrates;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment