31 lines
691 B
TypeScript
31 lines
691 B
TypeScript
import { open } from 'node:fs/promises';
|
|
|
|
|
|
const file = await open('./2025/inputs/3');
|
|
let count = 0
|
|
for await (const line of file.readLines()) {
|
|
count += findPair(line.split("").map(Number))
|
|
}
|
|
|
|
|
|
function findPair(numbs: Array<number>) {
|
|
let result = ''
|
|
for (var i = 12; i > 0; i--) {
|
|
const max = findDigit(numbs, i)
|
|
const maxIndex = numbs.indexOf(max)
|
|
numbs = numbs.slice(maxIndex + 1)
|
|
result += max
|
|
result
|
|
}
|
|
|
|
return Number(result)
|
|
}
|
|
|
|
function findDigit(numbs: Array<number>, pow: number) {
|
|
if (pow != 1) {
|
|
numbs = numbs.slice(0, -(pow - 1))
|
|
}
|
|
const max = Math.max(...numbs)
|
|
return max
|
|
}
|
|
console.log(count) |