Files
advent-of-code/AdventOfCode JS/2018/01/solution01a.js.old
T
Josh Creek 7054a23d8c Import AdventOfCode2018 JavaScript solutions
git-subtree-dir: AdventOfCode JS/2018
git-subtree-mainline: b44957b275
git-subtree-split: 5e9be1b66b
2026-07-12 19:58:48 +01:00

50 lines
1.5 KiB
Plaintext

// AdventOfCode2018-01
var freqChanges = [];
function parseInput(inputFile) {
// for each line in text file parse the symbol (first character) and the number into an array of objects
var fs = require('fs'),
readline = require('readline'),
instream = fs.createReadStream(inputFile),
outstream = new (require('stream'))(),
rl = readline.createInterface(instream, outstream);
rl.on('line', function (line) {
var object = {
operation: line.substr(0,1),
value: parseInt(line.substr(1))
}
freqChanges.push(object);
});
rl.on('close', function (line) {
//console.log(line);
//console.log('done reading file.');
});
}
parseInput('./input1.txt');
// Starting from zero, loop through all the entries in the array and apply the operations
var frequency = 0;
console.log('length=' + freqChanges.length);
freqChanges.forEach( function (object) {
console.log('Object contains ' + object.toString());
if (object.operation == '+') {
frequency += object.value;
}
else {
frequency -= object.value;
}
});
// for (i in freqChanges) { //(var i = 0; i < freqChanges.length; i++) {
// console.log(freqChanges[i].toString());
// if (freqChanges[i].operation = '+') {
// frequency += freqChanges[i].value;
// }
// else {
// frequency -= freqChanges[i].value;
// }
// }
console.log('The resulting frequency is ' + frequency);