From fae344e0604736fc5e84780dcac1d83852e1e908 Mon Sep 17 00:00:00 2001 From: Alix von Schirp Date: Thu, 12 Dec 2024 02:23:22 +0100 Subject: [PATCH] WIP-Day 9 Part 1 done, part 2 not working --- Day 09/Part 1/index.ts | 12 +++--- Day 09/Part 2/index.ts | 91 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 5 deletions(-) diff --git a/Day 09/Part 1/index.ts b/Day 09/Part 1/index.ts index 0f045d6..82f789b 100644 --- a/Day 09/Part 1/index.ts +++ b/Day 09/Part 1/index.ts @@ -19,16 +19,18 @@ export default function runner(input: string) { } for (let i = 0; i < fsblk.length; i++) { - while(fsblk[i] === -1) fsblk[i] = fsblk.pop()??-1 + while(fsblk[i] === -1) { + fsblk[i] = fsblk.pop()??-1 + if(fsblk.length === i+1) { + fsblk.pop() + break + } + } } - // console.log(fsblk) - let checksum = 0; - fsblk.forEach((value, index) => { checksum += value*index - // console.log(`${value} * ${index} = ${value*index} (total: ${checksum}`) }) console.log(checksum) diff --git a/Day 09/Part 2/index.ts b/Day 09/Part 2/index.ts index e69de29..91a2421 100644 --- a/Day 09/Part 2/index.ts +++ b/Day 09/Part 2/index.ts @@ -0,0 +1,91 @@ +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++; + } + + const spaces: Map = new Map() + + let currentIndex = 0; + let prevSpace = false; + + for (let i = 0; i < fsblk.length; i++) { + if(fsblk[i] === -1) { + if(prevSpace) { + spaces.set(currentIndex, (spaces.get(currentIndex)??0)+1) + } else { + currentIndex = i; + prevSpace = true; + spaces.set(i, 1) + } + } else { + prevSpace = false + } + } + + let blocks: Map = new Map() + + prevSpace = true + + for (let i = fsblk.length-1; i > -1; i--) { + if(fsblk[i] !== fsblk[i+1]) prevSpace = true + if(fsblk[i] !== -1) { + if(!prevSpace) { + blocks.set(currentIndex, (blocks.get(currentIndex)??0)+1) + } else { + currentIndex = i; + prevSpace = false; + blocks.set(currentIndex, 1) + } + } + } + + console.log(spaces, blocks) + + for(let fileId = id; fileId>-1; fileId++) { + const fileIndex = blocks.get(Array.from(blocks.keys())[fileId]??-1)??-1 + const fileSize = blocks.get(fileIndex??-1)??-1 + const freeBlockIndex = Array.from(spaces.keys())[Array.from(spaces.values()).findIndex(freeSpaceSize => freeSpaceSize>fileSize)]??-1 + + if(freeBlockIndex === -1) continue + + console.log(freeBlockIndex) + console.log(fsblk) + + for (let i = freeBlockIndex; i < freeBlockIndex + fileSize; i++) { + fsblk[i] = fileId + } + const newSpace = (spaces.get(freeBlockIndex)??0)-fileSize + + spaces.delete(freeBlockIndex) + if(newSpace > 0) spaces.set(freeBlockIndex+fileSize, newSpace) + + for (let i = 0; i < fileSize; i++) { + fsblk[fileIndex-1+i] = -1 + } + } + + let checksum = 0; + fsblk.forEach((value, index) => { + if(value === -1) return + checksum += value*index + }) + + console.log(checksum) + + }) +} \ No newline at end of file