36 lines
855 B
TypeScript
36 lines
855 B
TypeScript
import { open } from 'node:fs/promises';
|
|
|
|
|
|
const file = await open('./2025/inputs/6');
|
|
let count = 0
|
|
class Operation {
|
|
public numbers: number[]
|
|
public operation?: string
|
|
constructor() {
|
|
this.numbers = []
|
|
}
|
|
}
|
|
const operations: Operation[] = []
|
|
for await (const line of file.readLines()) {
|
|
const elements = line.matchAll(/(\S+)/g)
|
|
elements.forEach((e, i) => {
|
|
let op
|
|
if (operations.length < i + 1) {
|
|
op = new Operation()
|
|
operations.push(op)
|
|
} else {
|
|
op = operations[i]
|
|
}
|
|
if (Number.isNaN(Number(e[0]))) {
|
|
op.operation = e[0]
|
|
} else {
|
|
op.numbers.push(Number(e[0]))
|
|
}
|
|
})
|
|
}
|
|
|
|
const result = operations.reduce((acc, curr) => {
|
|
return acc + eval(curr.numbers.join(curr.operation))
|
|
}, 0)
|
|
|
|
console.log(result) |