Last active
January 21, 2022 15:41
-
-
Save dagda1/970a3a1ecec4dc7f7e6bd01a4930e253 to your computer and use it in GitHub Desktop.
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
// 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