Skip to content

Commit

Permalink
2024 day 7
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed Dec 7, 2024
1 parent 4f07240 commit 6e36f11
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
29 changes: 29 additions & 0 deletions 2024/07/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();
33 changes: 33 additions & 0 deletions 2024/07/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export async function taskOne(input: string[]): Promise<void> {
let sum = 0
for (const i of input) {
const p = i.split(': ')
const nums = p[1].split(' ').map(Number)
const goal = Number(p[0])
if (t(goal, nums, 1, nums[0])) sum += goal
}
console.log(sum)

function t(goal: number, nums: number[], i: number, cur: number): boolean {
if (i >= nums.length) return cur == goal
if (cur > goal) return false
return t(goal, nums, i+1, cur + nums[i]) || t(goal, nums, i+1, cur * nums[i])
}
}

export async function taskTwo(input: string[]): Promise<void> {
let sum = 0
for (const i of input) {
const p = i.split(': ')
const nums = p[1].split(' ').map(Number)
const goal = Number(p[0])
if (t(goal, nums, 1, nums[0])) sum += goal
}
console.log(sum)

function t(goal: number, nums: number[], i: number, cur: number): boolean {
if (i >= nums.length) return cur == goal
if (cur > goal) return false
return t(goal, nums, i+1, cur + nums[i]) || t(goal, nums, i+1, cur * nums[i]) || t(goal, nums, i+1, Number(cur.toString() + nums[i].toString()))
}
}
5 changes: 3 additions & 2 deletions 2024/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 2024
![](https://img.shields.io/badge/stars%20⭐-12-yellow)
![](https://img.shields.io/badge/stars%20⭐-14-yellow)

|Day|Language|Leaderboard (Part 1 / Part 2)|
|--|--|--|
Expand All @@ -8,4 +8,5 @@
|3|TypeScript|6107 / 2643|
|4|TypeScript|2103 / 1654|
|5|TypeScript|4252 / 4287|
|6|TypeScript|527 / 726|
|6|TypeScript|527 / 726|
|7|TypeScript|291 / 177|
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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⭐-12-yellow)
![](https://img.shields.io/badge/stars%20⭐-14-yellow)

|Day|Language|
|--|--|
Expand All @@ -19,6 +19,7 @@ Whatever I feel like today. But it will most likely either be Java, Python, C++,
|4|TypeScript|
|5|TypeScript|
|6|TypeScript|
|7|TypeScript|


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

0 comments on commit 6e36f11

Please sign in to comment.