Skip to content

Commit

Permalink
2024 day 13
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed Dec 13, 2024
1 parent 84e45e6 commit a2facc4
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 3 deletions.
29 changes: 29 additions & 0 deletions 2024/13/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();
106 changes: 106 additions & 0 deletions 2024/13/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import {lcmArr, lcm, gcd, ComplexNumber, Stack, Queue, JsonSet, FunctionSet, MinHeap } from '../../base'

export async function taskOne(input: string[]): Promise<void> {

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<void> {

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<State>()
const visited = new Set<string>()
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
}
*/
3 changes: 2 additions & 1 deletion 2024/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
|9|TypeScript|4996 / 4452|
|10|Typescript|673 / 442|
|11|Typescript|414 / 803|
|12|Typescript|1588 / 4633|
|12|Typescript|1588 / 4633|
|13|Typescript|675 / 1094|
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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⭐-24-yellow)
![](https://img.shields.io/badge/stars%20⭐-26-yellow)

|Day|Language|
|--|--|
Expand All @@ -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|


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

0 comments on commit a2facc4

Please sign in to comment.