-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.js
39 lines (30 loc) · 865 Bytes
/
solution.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
const fs = require('fs');
function calculateSteps(instructions) {
let steps = 0;
let position = 0;
while (position >= 0 && position < instructions.length) {
steps++;
const jump = instructions[position];
instructions[position]++;
position += jump;
}
return steps;
}
function processTraceFile(traceFilePath) {
const instructionSets = fs
.readFileSync(traceFilePath, 'utf-8')
.trim()
.split('\n');
let totalSteps = 0;
let stepsForLastSet = 0;
instructionSets.forEach((line, index) => {
const instructions = line.split(' ').map(Number);
const steps = calculateSteps(instructions);
totalSteps += steps;
if (index === instructionSets.length - 1) {
stepsForLastSet = steps;
}
});
return `submit ${totalSteps}-${stepsForLastSet}`;
}
module.exports = { processTraceFile, calculateSteps };