44 lines
1.0 KiB
TypeScript
44 lines
1.0 KiB
TypeScript
import fs from 'node:fs';
|
|
|
|
|
|
const file = fs.readFileSync('./2025/inputs/2').toString();
|
|
const ranges = file.split(",")
|
|
let invalids = 0
|
|
for (const r of ranges) {
|
|
const [first, last] = r.split("-")
|
|
processRange(Number(first), Number(last))
|
|
}
|
|
|
|
function processRange(start: number, end: number) {
|
|
for (var i = start; i <= end; i++) {
|
|
if (isInvalid(i)) {
|
|
invalids += i
|
|
}
|
|
}
|
|
}
|
|
|
|
function isInvalid(input: number) {
|
|
const strinput = input + ""
|
|
for (var i = Math.floor(strinput.length / 2); i > 0; i--) {
|
|
if ((strinput.length / i) % 1 != 0) {
|
|
// Not divisible
|
|
continue
|
|
}
|
|
const splitted = strSplit(strinput, i)
|
|
const first = splitted[0]
|
|
if (splitted.every(e => e == first)) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
function strSplit(str: string, n: number) {
|
|
var chunks = [];
|
|
|
|
for (var i = 0, charsLength = str.length; i < charsLength; i += n) {
|
|
chunks.push(str.substring(i, i + n));
|
|
}
|
|
return chunks
|
|
}
|
|
console.log(invalids) |