Last active
December 4, 2019 21:07
-
-
Save amezick/b03369216ac80aa3a7960eff1619b7cc to your computer and use it in GitHub Desktop.
Advent Of code 2019 day 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let nums=[]; | |
let count=0; | |
// "1" + 77777 - 99999 | |
for(let p1=7;p1<=9;p1++){ | |
for(let p2=p1;p2<=9;p2++){ | |
for(let p3=p2;p3<=9;p3++){ | |
for(let p4=p3;p4<=9;p4++){ | |
for(let p5=p4;p5<=9;p5++){ | |
nums.push("1"+p1+""+p2+""+p3+""+p4+""+p5); | |
} | |
} | |
} | |
} | |
} | |
//200000 - 599999 | |
for(let p1=2;p1<=5;p1++){ | |
for(let p2=p1;p2<=9;p2++){ | |
for(let p3=p2;p3<=9;p3++){ | |
for(let p4=p3;p4<=9;p4++){ | |
for(let p5=p4;p5<=9;p5++){ | |
for(let p6=p5;p6<=9;p6++){ | |
nums.push(p1+""+p2+""+p3+""+p4+""+p5+""+p6); | |
// count++; | |
} | |
} | |
} | |
} | |
} | |
} | |
//console.table(nums); | |
let tripleRegEx = new RegExp(/([0-9])\1{2,}/); | |
let tripleReplaceRegEx = new RegExp(/([0-9])\1{2,}/g); | |
let doublesRegEx = new RegExp(/([0-9])\1+/); | |
//for (let i = 0; i < nums.length; i++) { | |
// if(doubles.test(nums[i])){ | |
// count++; | |
// } | |
// | |
//} | |
let repeastsOnly=nums.filter(str => doublesRegEx.test(str)); | |
console.log("repeatsOnly: "+repeastsOnly.length); | |
let doublenums = nums.filter(str => tripleRegEx.test(str)); | |
//console.table(doublenums); | |
console.log("triple+ only: "+doublenums.length); | |
let triples= doublenums.map(s => s.replace(tripleReplaceRegEx,'')); | |
//console.table(triples); | |
console.log("3+ repeats removed from strings: "+triples.length); | |
let doublesWithoutTrip = triples.filter(str => doublesRegEx.test(str)); | |
//console.table(doublesWithoutTrip); | |
console.log("doublesWithoutTrip: "+ doublesWithoutTrip.length); | |
//1625 total | |
//876 with at least triples | |
console.log(repeastsOnly.length-doublenums.length+doublesWithoutTrip.length); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment