From 77bbbe3b81d460da26e44083b44a37533643594d Mon Sep 17 00:00:00 2001 From: Alex | Kronox Date: Thu, 5 Dec 2024 06:40:32 +0100 Subject: [PATCH] 2024 5 --- 2024/05/runner.ts | 29 +++++++++++++++++++++ 2024/05/task.ts | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 2024/README.md | 3 ++- README.md | 3 ++- 4 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 2024/05/runner.ts create mode 100644 2024/05/task.ts diff --git a/2024/05/runner.ts b/2024/05/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2024/05/runner.ts @@ -0,0 +1,29 @@ +import { readFile } from "fs/promises"; +import { taskOne, taskTwo } from "./task"; + +async function main() { + let lastArgument = process.argv.pop() as string; + let taskNumber = 1; + let isTest = false; + + if (lastArgument === "test") { + isTest = true; + taskNumber = parseInt(process.argv.pop() as string); + } else { + taskNumber = parseInt(lastArgument) + } + + const fileToLoad = isTest ? "test.in" : "solve.in"; + const fileContents = await readFile(fileToLoad, "utf-8") + + const lines = fileContents.split("\n"); + + if (taskNumber === 1) { + await taskOne(lines); + } + if (taskNumber === 2) { + await taskTwo(lines); + } +} + +void main(); \ No newline at end of file diff --git a/2024/05/task.ts b/2024/05/task.ts new file mode 100644 index 0000000..68c9705 --- /dev/null +++ b/2024/05/task.ts @@ -0,0 +1,66 @@ +export async function taskOne(input: string[]): Promise { + const before: Record = {}; + let i = 0; + while (input[i] != "") { + const nums = input[i].split("|").map(Number); + if (before[nums[0]] == undefined) before[nums[0]] = []; + before[nums[0]].push(nums[1]); + i++; + } + i++; + let s = 0; + while (i < input.length) { + const nums = input[i].split(",").map(Number); + let correct = true; + for (let i = 0; i < nums.length - 1; i++) { + if (before[nums[i]] == undefined) { + correct = false; + break; + } + for (let j = i + 1; j < nums.length; j++) { + if (!before[nums[i]].includes(nums[j])) correct = false; + } + } + if (correct) s += nums[Math.floor(nums.length / 2)]; + i++; + } + console.log(s); +} + +export async function taskTwo(input: string[]): Promise { + const before: Record = {}; + let i = 0; + while (input[i] != "") { + const nums = input[i].split("|").map(Number); + if (before[nums[0]] == undefined) before[nums[0]] = []; + before[nums[0]].push(nums[1]); + i++; + } + i++; + let s = 0; + while (i < input.length) { + let nums = input[i].split(",").map(Number); + let correct = true; + for (let i = 0; i < nums.length - 1; i++) { + if (before[nums[i]] == undefined) { + correct = false; + break; + } + for (let j = i + 1; j < nums.length; j++) { + if (!before[nums[i]].includes(nums[j])) correct = false; + } + } + if (!correct) { + nums = nums + .map((n) => [ + n, + (before[n] ?? []).filter((i) => nums.includes(i)).length, + ]) + .sort((a, b) => b[1] - a[1]) + .map((n) => n[0]); + s += nums[Math.floor(nums.length / 2)]; + } + i++; + } + console.log(s); +} diff --git a/2024/README.md b/2024/README.md index 856c134..a7c4a37 100644 --- a/2024/README.md +++ b/2024/README.md @@ -1,5 +1,5 @@ # 2024 -![](https://img.shields.io/badge/stars%20⭐-6-yellow) +![](https://img.shields.io/badge/stars%20⭐-10-yellow) |Day|Language|Leaderboard (Part 1 / Part 2)| |--|--|--| @@ -7,3 +7,4 @@ |2|TypeScript|3058 / 2617| |3|TypeScript|6107 / 2643| |4|TypeScript|2103 / 1654| +|5|TypeScript|4252 / 4287| \ No newline at end of file diff --git a/README.md b/README.md index 95c17fd..bea9145 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Whatever I feel like today. But it will most likely either be Java, Python, C++,

2024

-![](https://img.shields.io/badge/stars%20⭐-8-yellow) +![](https://img.shields.io/badge/stars%20⭐-10-yellow) |Day|Language| |--|--| @@ -17,6 +17,7 @@ Whatever I feel like today. But it will most likely either be Java, Python, C++, |2|TypeScript| |3|TypeScript| |4|TypeScript| +|5|TypeScript|

2023