From 65251c07ade5a7a132f093ee2642da851cb603be Mon Sep 17 00:00:00 2001 From: Izan Gil <66965250+SrIzan10@users.noreply.github.com> Date: Fri, 1 Dec 2023 23:25:52 +0100 Subject: [PATCH] feat: FINALLY 2ND PART SOLUTION --- 2023/src/cli.ts | 2 +- 2023/src/days/1/1.ts | 6 ++---- 2023/src/days/1/2.ts | 40 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/2023/src/cli.ts b/2023/src/cli.ts index b16d3ca..6cde16b 100644 --- a/2023/src/cli.ts +++ b/2023/src/cli.ts @@ -11,7 +11,7 @@ program const dirs = await readdir(`./src/days/${day}`, { withFileTypes: true }); const file = dirs.find((dir) => dir.name === `${option.part}.ts`); if (file) { - logger('success', `Running day ${day}`) + logger('success', `Running day ${day} part ${option.part}`) await import(`./days/${day}/${option.part}.ts`); } else { logger('error', `Day ${day} part ${option.part} not found!`); diff --git a/2023/src/days/1/1.ts b/2023/src/days/1/1.ts index 146e861..3965acb 100644 --- a/2023/src/days/1/1.ts +++ b/2023/src/days/1/1.ts @@ -5,6 +5,7 @@ const array: Array = [] const file = (await (Bun.file('./src/days/1/input.txt')).text()).split('\n') file.forEach((line) => { let arr = Array.from(line.replace(regex, '')).map(Number) + console.log(`${arr} ${line}`) if (arr.length === 0) return if (arr.length === 1) { // lol @@ -13,11 +14,8 @@ file.forEach((line) => { if (arr.length >= 2) { arr = [Number(`${arr[0]}${arr[arr.length - 1]}`)] } - let sum = 0 - arr.forEach(num => sum += num) - array.push(sum) + array.push(...arr) }) -console.log(array.length) let sum = 0 array.forEach(num => sum += num) diff --git a/2023/src/days/1/2.ts b/2023/src/days/1/2.ts index a29d0a1..e435c45 100644 --- a/2023/src/days/1/2.ts +++ b/2023/src/days/1/2.ts @@ -1 +1,39 @@ -console.log('ghjk') \ No newline at end of file +// REALLY UNCLEAN CODE BUT IT WORKS +const regex = /\D/g +const array: Array = [] + +let file = await (Bun.file('./src/days/1/input.txt')).text() +file = file.replaceAll("twone", "21") +file = file.replaceAll("sevenine", "79") +file = file.replaceAll("oneight", "18") +file = file.replaceAll("threeight", "38") +file = file.replaceAll("nineight", "98") +file = file.replaceAll("fiveight", "58") +file = file.replaceAll("eighthree", "83") +file = file.replaceAll("eightwo", "82") +file = file.replaceAll("one", "1") +file = file.replaceAll("two", "2") +file = file.replaceAll("three", "3") +file = file.replaceAll("four", "4") +file = file.replaceAll("five", "5") +file = file.replaceAll("six", "6") +file = file.replaceAll("seven", "7") +file = file.replaceAll("eight", "8") +file = file.replaceAll("nine", "9") +const fileSplit = file.split("\n"); + +fileSplit.forEach((line) => { + let arr = Array.from(line.replace(regex, '')).map(Number) + if (arr.length === 0) return + if (arr.length === 1) { + // lol + arr = [Number(`${arr[0]}${arr[0]}`)] + } + if (arr.length >= 2) { + arr = [Number(`${arr[0]}${arr[arr.length - 1]}`)] + } + array.push(...arr) +}) +let sum = 0 +array.forEach(num => sum += num) +console.log(sum) \ No newline at end of file