Created
December 4, 2019 14:06
-
-
Save ludekstepan/d37921dc72c5c2bd68fe9071e8482c8f to your computer and use it in GitHub Desktop.
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
const input = [240298, 784956] as const | |
function findCodes(min: number, max: number) { | |
const results: number[] = [] | |
next: | |
for (let i = min; i <= max; i++) { | |
const code = String(i) | |
let double = false | |
let adjCount = 1 | |
let last: number | undefined = undefined | |
for (const char of code) { | |
const num = Number(char) | |
if (num === last) { | |
adjCount++ | |
} else { | |
if (adjCount === 2) { | |
double = true | |
} | |
adjCount = 1 | |
} | |
if (last != null && num < last) { | |
continue next | |
} | |
last = num | |
} | |
if (adjCount === 2) { | |
double = true | |
} | |
if (double) { | |
results.push(i) | |
} | |
} | |
return results | |
} | |
const codes = findCodes(...input) | |
console.log(codes.length) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment