From 98bf0f9dd1783beee24f5bdf3b774f19ba1d8259 Mon Sep 17 00:00:00 2001 From: TobenderZephyr Date: Sun, 22 Dec 2024 13:33:21 +0100 Subject: [PATCH] day09 pre-fragmentation. parsing successful. --- 2024/09/easy_example.txt | 1 + 2024/09/example.txt | 1 + 2024/09/index.js | 35 +++++++++++++---------------------- 2024/09/input.txt | 0 2024/09/package.json | 11 ----------- 5 files changed, 15 insertions(+), 33 deletions(-) create mode 100644 2024/09/easy_example.txt create mode 100644 2024/09/example.txt create mode 100644 2024/09/input.txt delete mode 100644 2024/09/package.json diff --git a/2024/09/easy_example.txt b/2024/09/easy_example.txt new file mode 100644 index 0000000..bd41cba --- /dev/null +++ b/2024/09/easy_example.txt @@ -0,0 +1 @@ +12345 \ No newline at end of file diff --git a/2024/09/example.txt b/2024/09/example.txt new file mode 100644 index 0000000..5ff5aae --- /dev/null +++ b/2024/09/example.txt @@ -0,0 +1 @@ +2333133121414131402 \ No newline at end of file diff --git a/2024/09/index.js b/2024/09/index.js index bc40a89..d809ba2 100644 --- a/2024/09/index.js +++ b/2024/09/index.js @@ -1,26 +1,17 @@ -let disk_map = "2333133121414131402" -// 0..111....22222 +import fs from 'node:fs' -//let fileArray = Array.from(file) +function init(filename) { + const data = fs.readFileSync(filename, { encoding: 'utf-8' }, data => data) -output = "" -hints = "" -//output += "".padStart(disk_map[0], 0) -let id = -1 -let is_file = false -for (let idx = 0; idx < disk_map.length; idx++) { - let length = disk_map[idx] - is_file = !is_file - id = is_file ? id + 1 : id - output += "".padStart(length, (is_file ? id : '.')) - new_disk_map = + let table = "" + let files = [...data.split('').filter((val, idx) => idx % 2 === 0)] + let free_spaces = [...data.split('').filter((val, idx) => idx % 2 !== 0)] + let diskmap = [...data.split('')] + + diskmap.forEach((sector, index) => { + table += index % 2 === 0 ? ''.padStart(sector, index / 2) : ''.padStart(sector, '.') + }) + console.log(table) } -function frag() { - -} - - -console.log(disk_map) -//console.log(hints) -console.log(output) \ No newline at end of file +init('easy_example.txt') \ No newline at end of file diff --git a/2024/09/input.txt b/2024/09/input.txt new file mode 100644 index 0000000..e69de29 diff --git a/2024/09/package.json b/2024/09/package.json deleted file mode 100644 index db9a9ba..0000000 --- a/2024/09/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "09", - "version": "1.0.0", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "", - "license": "ISC", - "description": "" -}