day 5
This commit is contained in:
+63
@@ -0,0 +1,63 @@
|
||||
import assert from 'node:assert';
|
||||
import fs from 'node:fs';
|
||||
|
||||
|
||||
const file = fs.readFileSync('./2025/inputs/6').toString();
|
||||
let count = 0
|
||||
class Operation {
|
||||
public numbers: number[]
|
||||
public operation?: string
|
||||
constructor() {
|
||||
this.numbers = []
|
||||
}
|
||||
}
|
||||
const operations: Operation[] = []
|
||||
const reversed: string[] = []
|
||||
for (const line of file.split("\n")) {
|
||||
reversed.push(line.split('').toReversed().join(""))
|
||||
}
|
||||
|
||||
let currentOperation = new Operation()
|
||||
operations.push(currentOperation)
|
||||
reversed[0].split('').forEach((_, lineN) => {
|
||||
let currentNumber: string[] = []
|
||||
let s = ""
|
||||
reversed.forEach((_, colN) => {
|
||||
s = reversed[colN][lineN]
|
||||
|
||||
if (s == " ") {
|
||||
return
|
||||
}
|
||||
|
||||
const n = Number(s)
|
||||
if (isNaN(n)) {
|
||||
return
|
||||
}
|
||||
currentNumber.push(s)
|
||||
})
|
||||
|
||||
if (currentNumber.length > 0) {
|
||||
currentOperation.numbers.push(Number(currentNumber.join("")))
|
||||
}
|
||||
|
||||
currentNumber = []
|
||||
|
||||
if (s == "*" || s == "+") {
|
||||
currentOperation.operation = s
|
||||
if (currentNumber.length > 0) {
|
||||
currentOperation.numbers.push(Number(currentNumber.join("")))
|
||||
}
|
||||
|
||||
currentNumber = []
|
||||
currentOperation = new Operation()
|
||||
operations.push(currentOperation)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
operations.pop()
|
||||
const result = operations.reduce((acc, curr) => {
|
||||
return acc + eval(curr.numbers.join(curr.operation))
|
||||
}, 0)
|
||||
|
||||
console.log(result)
|
||||
Reference in New Issue
Block a user