WIP-Day 9

This commit is contained in:
2024-12-11 22:10:52 +01:00
parent 6d2dc6764a
commit 48f1604ed2
2 changed files with 37 additions and 0 deletions

37
Day 09/Part 1/index.ts Normal file
View File

@@ -0,0 +1,37 @@
import fs from 'node:fs';
export default function runner(input: string) {
const fsblk: number[]= []
let id = 0;
fs.readFile(`./${input}`, 'utf8', (err, data) => {
if (err) throw err;
for (let i = 0; i < data.length; i++) {
for(let j = 0; j < parseInt(data.charAt(i)); j++) {
if(i%2 == 0){
fsblk.push(id)
} else {
fsblk.push(-1)
}
}
if(i%2 == 0) id++;
}
for (let i = 0; i < fsblk.length; i++) {
while(fsblk[i] === -1) fsblk[i] = fsblk.pop()??-1
}
// console.log(fsblk)
let checksum = 0;
fsblk.forEach((value, index) => {
checksum += value*index
// console.log(`${value} * ${index} = ${value*index} (total: ${checksum}`)
})
console.log(checksum)
})
}