Skip to content

Commit

Permalink
2024 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed Dec 5, 2024
1 parent 7f60937 commit 77bbbe3
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
29 changes: 29 additions & 0 deletions 2024/05/runner.ts
Original file line number Diff line number Diff line change
@@ -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();
66 changes: 66 additions & 0 deletions 2024/05/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
export async function taskOne(input: string[]): Promise<void> {
const before: Record<number, number[]> = {};
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<void> {
const before: Record<number, number[]> = {};
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);
}
3 changes: 2 additions & 1 deletion 2024/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# 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)|
|--|--|--|
|1|TypeScript|558 / 897|
|2|TypeScript|3058 / 2617|
|3|TypeScript|6107 / 2643|
|4|TypeScript|2103 / 1654|
|5|TypeScript|4252 / 4287|
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ Whatever I feel like today. But it will most likely either be Java, Python, C++,

<h3>2024</h3>

![](https://img.shields.io/badge/stars%20⭐-8-yellow)
![](https://img.shields.io/badge/stars%20⭐-10-yellow)

|Day|Language|
|--|--|
|1|TypeScript|
|2|TypeScript|
|3|TypeScript|
|4|TypeScript|
|5|TypeScript|


<h3><a href="2023/README.md">2023</a></h3>
Expand Down

0 comments on commit 77bbbe3

Please sign in to comment.