const{readFile}=require('./readLines');(async()=>{constlines=awaitreadFile('02-input.txt');lettwoLettersCount=0;letthreeLettersCount=0;for(letlineoflines){constfrequencyMap={};for(constcharofline.split('')){frequencyMap[char]=(frequencyMap[char]||0)+1;}consthasTwoLetters=Object.values(frequencyMap).some(frequency=>frequency===2);consthasThreeLetters=Object.values(frequencyMap).some(frequency=>frequency===3);twoLettersCount+=+hasTwoLetters;threeLettersCount+=+hasThreeLetters;};constchecksum=twoLettersCount*threeLettersCount;console.log(`The checksum is ${checksum}`);})();
02b.js
const{readFile}=require('./readLines');// Compares two strings to see if they differ by one char and which one functioncompare(string1,string2){constlength=string1.length;letdifferentChars=0;letdifferIndex;for(leti=0;i<length;i++){if(string1.charAt(i)!==string2.charAt(i)){differentChars++;differIndex=differentChars===1?i:undefined;}}return{differByOneChar:differentChars===1,differIndex};}// Compare each strings to every other string // and get the common letters in case the differByOneChar is truefunctiongetCommonLetters(ids){constidsCount=ids.length;for(leti=0;i<idsCount;i++){constid=ids[i];for(letj=i+1;j<idsCount;j++){const{differByOneChar,differIndex}=compare(id,ids[j]);if(differByOneChar){returnid.slice(0,differIndex)+id.slice(differIndex+1);}}}}(async()=>{constlines=awaitreadFile('02-input.txt');constcommonLetters=getCommonLetters(lines);console.log(`The common letters are ${commonLetters}`);})();
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
My solution in JavaScript / Node 11, using the
readlineinterface:readLines.js
02a.js
02b.js