-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththe-grid-bot-problem.js
67 lines (60 loc) · 2.08 KB
/
the-grid-bot-problem.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var fs = require('fs');
var filePath = process.argv[2];
var gridProblemSolver = require('./the-grid-problem-solver.js');
if (filePath === undefined) {
var error = 'Please enter the path of input file';
console.log(error);
return error;
}
if(!fs.existsSync(filePath)){
var error = 'filePath does not exist';
console.log(error);
return error;
}
var buffer = fs.readFileSync(filePath);
var contentOfFile = buffer.toString();
var inputArray = contentOfFile.split('\n');
var gridWithStringElements = (inputArray[0].split(' '));
var isGridInputValid = true;
var grid = gridWithStringElements.map(function (element) {
var element = Number(element);
if (isNaN(element) === true) {
isGridInputValid = false;
}
return element;
});
if (!isGridInputValid) {
var error = 'The grid size entered are not a number. Please check your input file';
console.log(error);
return error;
}
var initialBotPosition = (inputArray[1].split(' '));
var initialX = Number(initialBotPosition[0]);
if (isNaN(initialX) === true) {
var error = 'The initial bot -> X position entered is not a number. Please check your input file';
console.log(error);
return error;
}
var initialY = Number(initialBotPosition[1]);
if (isNaN(initialY) === true) {
var error = 'The initial bot -> Y position entered is not a number. Please check your input file';
console.log(error);
return error;
}
var initialDirection = initialBotPosition[2];
if (initialDirection !== 'N' && initialDirection !== 'S' && initialDirection !== 'E' && initialDirection !== 'W') {
var error = 'The initial bot -> direction entered is not valid. Please check your input file';
console.log(error);
return error;
}
initialBotPosition[0] = initialX;
initialBotPosition[1] = initialY;
var directionCommands = inputArray[2];
var pattern = /[^LRM]/;
if (pattern.test(directionCommands)) {
var error = 'Invalid command string. Your string contains characters other than L or R or M. Please check your input file';
console.log(error);
return error;
}
var result = gridProblemSolver(grid, initialBotPosition, directionCommands);
console.log(result);