Skip to content

Instantly share code, notes, and snippets.

@dagda1
Last active January 21, 2022 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dagda1/970a3a1ecec4dc7f7e6bd01a4930e253 to your computer and use it in GitHub Desktop.
Save dagda1/970a3a1ecec4dc7f7e6bd01a4930e253 to your computer and use it in GitHub Desktop.
// HERE IS MY ANSWER WITH THE TIME CONSTRAINT REMOVED
// https://github.com/dagda1/guardian-interview/blob/v0/src/index.ts
import inquirer from 'inquirer';
const { prompt } = inquirer;
const commands = ['N', 'S', 'E', 'W'] as const;
type RobotState = 'IDLE' | 'CARRYING';
type Commands = typeof commands[number];
const MaxColumns = 10;
const MaxRows = 10;
interface Robot {
move(command: Commands): void;
carry(): void;
drop(): void;
state: RobotState;
}
type Point = [number, number];
interface Warehouse {
grid: Point[][];
robot: Robot;
current: Point;
}
function createWarehouse(): Warehouse {
let grid: Warehouse['grid'] = [];
for (let row = 0; row < MaxRows; row++) {
grid[row] = [];
for (let column = 0; column < MaxColumns; column++) {
grid[row][column] = [row, column];
}
}
return {
grid,
robot: {
state: 'IDLE',
carry() {
if (this.state === 'CARRYING') {
console.log(`no you don't`);
return;
}
this.state = 'CARRYING';
},
drop() {
this.state = 'IDLE';
},
move(command: Commands) {
console.dir(command, { depth: 33 });
},
},
current: [0, 0],
};
}
export async function main(): Promise<void> {
const warehouse: Warehouse = createWarehouse();
while (true) {
const { action } = await prompt([
{
type: 'list',
name: 'action',
message: 'What do you want to do?',
choices: commands,
},
]);
const [x, y] = warehouse.current;
switch (action as Commands) {
case 'N':
if (y < MaxRows) {
warehouse.current = [x, y + 1];
}
break;
case 'S':
if (y !== 0) {
warehouse.current = [x, y - 1];
}
break;
case 'E':
if (x < MaxColumns) {
warehouse.current = [x + 1, y];
}
break;
case 'W':
if (x !== 0) {
warehouse.current = [x - 1, y];
}
}
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment