Files
advent-of-code/AdventOfCode JS/2018/02/solution01.js
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

48 lines
1.5 KiB
JavaScript

// AdventOfCode2018-02
var twiceCounter = 0;
var thriceCounter = 0;
// Import the input file
var fs = require('fs');
var frequency = 0;
var textFile = fs.readFileSync('./input.txt').toString();
var textArray = textFile.split(/\r?\n/);
// For each line of the input file
textArray.forEach(line => {
var searchCharHistory = [];
var lineTwiceCounter = 0;
var lineThriceCounter = 0;
for (i = 0; i < line.length; i++) {
searchChar = line.charAt(i);
if (!searchCharHistory.indexOf(searchChar) >= 0) {
// If we haven't already counted this letter
//console.log('Searching for ' + searchChar + ' in line ' + line);
var re = new RegExp(searchChar, 'g');
var matchCharCount = (line.match(re) || []).length;
//console.log(matchCharCount);
// Check if a letter appears twice (and increment the counter)
if (matchCharCount == 2) {
lineTwiceCounter += 1;
}
// Check if a letter appears three times (and increment the counter)
else if (matchCharCount == 3) {
lineThriceCounter += 1;
}
}
searchCharHistory.push(searchChar);
}
if (lineTwiceCounter > 0) {
twiceCounter += 1;
}
if (lineThriceCounter > 0) {
thriceCounter += 1;
}
});
console.log(twiceCounter);
console.log(thriceCounter);
// multiply the two counters together
console.log('The checksum is ' + (twiceCounter * thriceCounter));