Skip to content

Instantly share code, notes, and snippets.

@ludekstepan
Created December 4, 2019 14:06
Show Gist options
  • Save ludekstepan/d37921dc72c5c2bd68fe9071e8482c8f to your computer and use it in GitHub Desktop.
Save ludekstepan/d37921dc72c5c2bd68fe9071e8482c8f to your computer and use it in GitHub Desktop.
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