42 lines
1.1 KiB
TypeScript
42 lines
1.1 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(ogstart: number, ogend: number) {
|
|
let start = ogstart
|
|
let end = ogend
|
|
let startDigitAmount = Math.floor(Math.log10(start))
|
|
if ((startDigitAmount + 1) % 2 == 1) {
|
|
start = 10 ** (startDigitAmount + 1)
|
|
startDigitAmount++
|
|
}
|
|
let firstStartHalf = Math.floor(start / (10 ** Math.floor((startDigitAmount + 1) / 2)))
|
|
|
|
let endDigitAmount = Math.floor(Math.log10(end))
|
|
if ((endDigitAmount + 1) % 2 == 1) {
|
|
end = (10 ** (endDigitAmount)) - 1
|
|
endDigitAmount--
|
|
}
|
|
const firstEndHalf = Math.floor(end / (10 ** Math.floor((endDigitAmount + 1) / 2)))
|
|
|
|
for (var i = firstStartHalf; i <= firstEndHalf; i++) {
|
|
if (Math.floor(Math.log10(start)) % 2 == 0) {
|
|
continue
|
|
}
|
|
const n = Number(i + "" + i)
|
|
if (ogstart <= n && n <= ogend) {
|
|
invalids += n
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log(invalids) |