-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
35 lines (26 loc) · 900 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const fs = require('fs');
const { getGrid, getRobots } = require('./src/inputTranslator.js');
const { driveRobot } = require('./src/driver.js');
function instructRobots(file) {
const input = fs.readFileSync(file, 'utf-8');
if (!input) {
throw new Error('Please provide some instructions in `instructions.txt`');
}
const instructionsArray = input.split('\n').filter(instruction => !!instruction);
const [inputGrid, ...inputRobots] = instructionsArray;
const grid = getGrid(inputGrid);
const robots = getRobots(inputRobots);
let output = [];
robots.forEach(robot => {
const { x, y, orientation, isLost } = driveRobot(robot, grid);
output.push(`${ x } ${ y } ${ orientation }${ isLost ? ' LOST' : '' }`);
});
return output;
}
function init(file) {
instructRobots(file).forEach(output => console.log(output));
}
module.exports = {
init,
instructRobots
};