From a2facc4516c7de64c607ec65eecd9284def6029f Mon Sep 17 00:00:00 2001 From: Alex | Kronox Date: Fri, 13 Dec 2024 07:00:30 +0100 Subject: [PATCH] 2024 day 13 --- 2024/13/runner.ts | 29 +++++++++++++ 2024/13/task.ts | 106 ++++++++++++++++++++++++++++++++++++++++++++++ 2024/README.md | 3 +- README.md | 5 ++- 4 files changed, 140 insertions(+), 3 deletions(-) create mode 100644 2024/13/runner.ts create mode 100644 2024/13/task.ts diff --git a/2024/13/runner.ts b/2024/13/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2024/13/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/13/task.ts b/2024/13/task.ts new file mode 100644 index 0000000..8a01798 --- /dev/null +++ b/2024/13/task.ts @@ -0,0 +1,106 @@ +import {lcmArr, lcm, gcd, ComplexNumber, Stack, Queue, JsonSet, FunctionSet, MinHeap } from '../../base' + +export async function taskOne(input: string[]): Promise { + + let i = 0 + const buttons: any[] = [] + while(i < input.length) { + const ra = /Button A: X([0-9+-]*), Y([0-9+-]*)/.exec(input[i])! + const rb = /Button B: X([0-9+-]*), Y([0-9+-]*)/.exec(input[i+1])! + const rg = /Prize: X=([0-9+-]*), Y=([0-9+-]*)/.exec(input[i+2])! + i+= 4 + buttons.push({ + ax: Number(ra[1]), + ay: Number(ra[2]), + bx: Number(rb[1]), + by: Number(rb[2]), + x: Number(rg[1]), + y: Number(rg[2]) + }) + } + function getPrize(r: any) { + let min = Infinity + for (let j = 0; j <= 100; j++) { + for (let bs = 0; bs <= 100; bs++) { + if (j * r.ax + bs * r.bx != r.x) continue + if (j * r.ay + bs * r.by != r.y) continue + const cost = j*3+bs + if (cost < min) min = cost + } + } + return min + } + + console.log(buttons.map(getPrize).filter(i => i != Infinity).reduce((a,b)=>a+b,0)) +} + +export async function taskTwo(input: string[]): Promise { + + let i = 0 + const buttons: any[] = [] + while(i < input.length) { + const ra = /Button A: X([0-9+-]*), Y([0-9+-]*)/.exec(input[i])! + const rb = /Button B: X([0-9+-]*), Y([0-9+-]*)/.exec(input[i+1])! + const rg = /Prize: X=([0-9+-]*), Y=([0-9+-]*)/.exec(input[i+2])! + i+= 4 + buttons.push({ + ax: Number(ra[1]), + ay: Number(ra[2]), + bx: Number(rb[1]), + by: Number(rb[2]), + x: Number(rg[1]) + 10000000000000, + y: Number(rg[2]) + 10000000000000 + }) + } + function getPrize(r: any) { + const b = (r.y - r.x * (r.ay / r.ax)) / (r.by - r.bx * (r.ay / r.ax)) + const rb = round(b) + if (isNaN(b) || rb == Infinity) return Infinity + const a = (r.x - rb * r.bx) / r.ax + const ra = round(a) + if (isNaN(a) || ra == Infinity) return Infinity + return rb+ra*3 + } + + function round(x: number) { + const r = Math.round(x) + if (Math.abs(r - x) < 0.001) return r + return Infinity + } + + console.log(buttons.map(getPrize).filter(i => i != Infinity).reduce((a,b)=>a+b,0)) +} + + + +/* +const grid = input.map(i => i.split('')) + +for (let y = 0; y < grid.length; y++) { + for (let x = 0; x < grid[y].length; x++) { + + } +} + + +const queue = new Queue() +const visited = new Set() +queue.push() + +while(!queue.isEmpty()) { + const q = queue.pop() + const k = q.x + '|' + q.y + if (visited.has(k)) continue + visited.add(k) + + for () { + queue.push() + } +} + +interface State { + x: number + y: number +} + +*/ diff --git a/2024/README.md b/2024/README.md index 56729d3..b62c85b 100644 --- a/2024/README.md +++ b/2024/README.md @@ -14,4 +14,5 @@ |9|TypeScript|4996 / 4452| |10|Typescript|673 / 442| |11|Typescript|414 / 803| -|12|Typescript|1588 / 4633| \ No newline at end of file +|12|Typescript|1588 / 4633| +|13|Typescript|675 / 1094| \ No newline at end of file diff --git a/README.md b/README.md index 6ae1bcf..f98991c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # My Advent of Code Solutions -![](https://img.shields.io/badge/Total%20stars%20⭐-471yellow) +![](https://img.shields.io/badge/Total%20stars%20⭐-473yellow) ## What language do I use? Whatever I feel like today. But it will most likely either be Java, Python, C++, Typescript or Haskell @@ -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⭐-24-yellow) +![](https://img.shields.io/badge/stars%20⭐-26-yellow) |Day|Language| |--|--| @@ -25,6 +25,7 @@ Whatever I feel like today. But it will most likely either be Java, Python, C++, |10|TypeScript| |11|TypeScript| |12|TypeScript| +|13|TypeScript|

2023