Skip to content

Commit

Permalink
2024D13: Part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
martapanc committed Dec 13, 2024
1 parent 1921710 commit c1a3cd2
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 2024/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"date-fns": "^4.1.0",
"immutable": "^5.0.3",
"jest": "^29.7.0",
"mathjs": "^14.0.1",
"ml-matrix": "^6.12.0",
"ts-node": "^10.9.2",
"typescript": "^5.7.2"
},
Expand Down
13 changes: 13 additions & 0 deletions 2024/src/2024/day13/day13.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {part1, part2} from "./day13";

describe('2024 Day 13', () => {
test('Part 1', async () => {
expect(await part1('testInput1')).toEqual(480);
expect(await part1('input')).toEqual(37297);
});

test('Part 2', async () => {
expect(await part2('testInput1')).toEqual(31);
expect(await part2('input')).toEqual(29379307);
});
});
74 changes: 74 additions & 0 deletions 2024/src/2024/day13/day13.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import path from "node:path";
import {readInputLineByLine} from "@utils/io";
import {Coord} from "@utils/grid";
import {Matrix, solve} from 'ml-matrix';

export async function part1(inputFile: string) {
return await day13(inputFile, playAllMachines);
}

export async function part2(inputFile: string) {
return await day13(inputFile);
}

type ClawMachine = {
buttonA: Coord;
buttonB: Coord;
prize: Coord;
}

async function day13(inputFile: string, calcFn?: (clawMachines: ClawMachine[]) => number) {
const inputPath = path.join(__dirname, inputFile);
const lines = await readInputLineByLine(inputPath);

const clawMachines: ClawMachine[] = [];

const parseNums = (line: string) => [...line.match(/\d+/g)!].map(s => Number.parseInt(s));

lines.forEach((line, index) => {
if (line.includes("Button A")) {
const a = parseNums(line);
const buttonA = new Coord(a[0], a[1]);
const b = parseNums(lines[index + 1]);
const buttonB = new Coord(b[0], b[1]);
const p = parseNums(lines[index + 2]);
const prize = new Coord(p[0], p[1]);
clawMachines.push({ buttonA, buttonB, prize })
}
});

return calcFn?.(clawMachines);
}

function playAllMachines(clawMachines: ClawMachine[]): number {
let totalTokens = 0;

clawMachines.forEach(clawMachine => {
const { buttonA, buttonB, prize } = clawMachine;

const coeffMatrix = new Matrix([
[buttonA.x, buttonB.x],
[buttonA.y, buttonB.y]
]);

const resultsVector = Matrix.columnVector([prize.x, prize.y]);

const x = solve(coeffMatrix, resultsVector);
// const error = Matrix.sub(resultsVector, coeffMatrix.mmul(x));

const solution = x.to1DArray().map(v => roundCloseToInteger(v));
if (solution[0] && solution[1]) {
totalTokens += solution[0] * 3 + solution[1] * 1
}
});

return totalTokens;
}

function roundCloseToInteger(num: number, tolerance = 1e-9) {
const nearestInt = Math.round(num);
if (Math.abs(num - nearestInt) <= tolerance) {
return nearestInt;
}
return undefined;
}
15 changes: 15 additions & 0 deletions 2024/src/2024/day13/testInput1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Button A: X+94, Y+34
Button B: X+22, Y+67
Prize: X=8400, Y=5400

Button A: X+26, Y+66
Button B: X+67, Y+21
Prize: X=12748, Y=12176

Button A: X+17, Y+86
Button B: X+84, Y+37
Prize: X=7870, Y=6450

Button A: X+69, Y+23
Button B: X+27, Y+71
Prize: X=18641, Y=10279
103 changes: 103 additions & 0 deletions 2024/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@
dependencies:
"@babel/helper-plugin-utils" "^7.25.9"

"@babel/runtime@^7.25.7":
version "7.26.0"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.26.0.tgz#8600c2f595f277c60815256418b85356a65173c1"
integrity sha512-FDSOghenHTiToteC/QRlv2q3DhPZ/oOXTBoirfWNx1Cx3TMVcGWQtMMmQcSvb/JjpNeGzx8Pq/b4fKEJuWm1sw==
dependencies:
regenerator-runtime "^0.14.0"

"@babel/template@^7.25.9", "@babel/template@^7.3.3":
version "7.25.9"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.25.9.tgz#ecb62d81a8a6f5dc5fe8abfc3901fc52ddf15016"
Expand Down Expand Up @@ -1068,6 +1075,11 @@ command-line-args@^6.0.1:
lodash.camelcase "^4.3.0"
typical "^7.2.0"

complex.js@^2.2.5:
version "2.4.2"
resolved "https://registry.yarnpkg.com/complex.js/-/complex.js-2.4.2.tgz#76f260a9e7e232d8ad26348484a9b128c13fcc9a"
integrity sha512-qtx7HRhPGSCBtGiST4/WGHuW+zeaND/6Ld+db6PbrulIB1i2Ev/2UPiqcmpQNPSyfBKraC0EOvOKCB5dGZKt3g==

[email protected]:
version "0.0.1"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
Expand Down Expand Up @@ -1117,6 +1129,11 @@ debug@^4.1.0, debug@^4.1.1, debug@^4.3.1:
dependencies:
ms "^2.1.3"

decimal.js@^10.4.3:
version "10.4.3"
resolved "https://registry.yarnpkg.com/decimal.js/-/decimal.js-10.4.3.tgz#1044092884d245d1b7f65725fa4ad4c6f781cc23"
integrity sha512-VBBaLc1MgL5XpzgIP7ny5Z6Nx3UrRkIViUkPUdtl9aya5amy3De1gsUUSB1g3+3sExYNjCAsAznmukyxCb1GRA==

dedent@^1.0.0:
version "1.5.3"
resolved "https://registry.yarnpkg.com/dedent/-/dedent-1.5.3.tgz#99aee19eb9bae55a67327717b6e848d0bf777e5a"
Expand Down Expand Up @@ -1211,6 +1228,11 @@ escalade@^3.1.1, escalade@^3.2.0:
resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.2.0.tgz#011a3f69856ba189dffa7dc8fcce99d2a87903e5"
integrity sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA==

escape-latex@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/escape-latex/-/escape-latex-1.2.0.tgz#07c03818cf7dac250cce517f4fda1b001ef2bca1"
integrity sha512-nV5aVWW1K0wEiUIEdZ4erkGGH8mDxGyxSeqPzRNtWP7ataw+/olFObw7hujFWlVjNsaDFw5VZ5NzVSIqRgfTiw==

escape-string-regexp@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-2.0.0.tgz#a30304e99daa32e23b2fd20f51babd07cffca344"
Expand Down Expand Up @@ -1305,6 +1327,11 @@ form-data@^4.0.0:
combined-stream "^1.0.8"
mime-types "^2.1.12"

fraction.js@^5.2.1:
version "5.2.1"
resolved "https://registry.yarnpkg.com/fraction.js/-/fraction.js-5.2.1.tgz#93ffc9507b1a68a1271883aa28e98f58a1c0c6b3"
integrity sha512-Ah6t/7YCYjrPUFUFsOsRLMXAdnYM+aQwmojD2Ayb/Ezr82SwES0vuyQ8qZ3QO8n9j7W14VJuVZZet8U3bhSdQQ==

fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
Expand Down Expand Up @@ -1422,6 +1449,11 @@ inherits@2:
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==

is-any-array@^2.0.0, is-any-array@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/is-any-array/-/is-any-array-2.0.1.tgz#9233242a9c098220290aa2ec28f82ca7fa79899e"
integrity sha512-UtilS7hLRu++wb/WBAw9bNuP1Eg04Ivn1vERJck8zJthEvXCBEBpGR/33u/xLKWEQf95803oalHrVDptcAvFdQ==

is-arrayish@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/is-arrayish/-/is-arrayish-0.2.1.tgz#77c99840527aa8ecb1a8ba697b80645a7a926a9d"
Expand Down Expand Up @@ -1522,6 +1554,11 @@ jake@^10.8.5:
filelist "^1.0.4"
minimatch "^3.1.2"

javascript-natural-sort@^0.7.1:
version "0.7.1"
resolved "https://registry.yarnpkg.com/javascript-natural-sort/-/javascript-natural-sort-0.7.1.tgz#f9e2303d4507f6d74355a73664d1440fb5a0ef59"
integrity sha512-nO6jcEfZWQXDhOiBtG2KvKyEptz7RVbpGP4vTD2hLBdmNQSsCiicO2Ioinv6UI4y9ukqnBpy+XZ9H6uLNgJTlw==

jest-changed-files@^29.7.0:
version "29.7.0"
resolved "https://registry.yarnpkg.com/jest-changed-files/-/jest-changed-files-29.7.0.tgz#1c06d07e77c78e1585d020424dedc10d6e17ac3a"
Expand Down Expand Up @@ -1966,6 +2003,21 @@ [email protected]:
dependencies:
tmpl "1.0.5"

mathjs@^14.0.1:
version "14.0.1"
resolved "https://registry.yarnpkg.com/mathjs/-/mathjs-14.0.1.tgz#b47233a3e0913ae3d2669d67f4edf7a5b6fe1fb1"
integrity sha512-yyJgLwC6UXuve724np8tHRMYaTtb5UqiOGQkjwbSXgH8y1C/LcJ0pvdNDZLI2LT7r+iExh2Y5HwfAY+oZFtGIQ==
dependencies:
"@babel/runtime" "^7.25.7"
complex.js "^2.2.5"
decimal.js "^10.4.3"
escape-latex "^1.2.0"
fraction.js "^5.2.1"
javascript-natural-sort "^0.7.1"
seedrandom "^3.0.5"
tiny-emitter "^2.1.0"
typed-function "^4.2.1"

merge-stream@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60"
Expand Down Expand Up @@ -2010,6 +2062,37 @@ minimatch@^5.0.1:
dependencies:
brace-expansion "^2.0.1"

ml-array-max@^1.2.4:
version "1.2.4"
resolved "https://registry.yarnpkg.com/ml-array-max/-/ml-array-max-1.2.4.tgz#2373e2b7e51c8807e456cc0ef364c5863713623b"
integrity sha512-BlEeg80jI0tW6WaPyGxf5Sa4sqvcyY6lbSn5Vcv44lp1I2GR6AWojfUvLnGTNsIXrZ8uqWmo8VcG1WpkI2ONMQ==
dependencies:
is-any-array "^2.0.0"

ml-array-min@^1.2.3:
version "1.2.3"
resolved "https://registry.yarnpkg.com/ml-array-min/-/ml-array-min-1.2.3.tgz#662f027c400105816b849cc3cd786915d0801495"
integrity sha512-VcZ5f3VZ1iihtrGvgfh/q0XlMobG6GQ8FsNyQXD3T+IlstDv85g8kfV0xUG1QPRO/t21aukaJowDzMTc7j5V6Q==
dependencies:
is-any-array "^2.0.0"

ml-array-rescale@^1.3.7:
version "1.3.7"
resolved "https://registry.yarnpkg.com/ml-array-rescale/-/ml-array-rescale-1.3.7.tgz#c4d129320d113a732e62dd963dc1695bba9a5340"
integrity sha512-48NGChTouvEo9KBctDfHC3udWnQKNKEWN0ziELvY3KG25GR5cA8K8wNVzracsqSW1QEkAXjTNx+ycgAv06/1mQ==
dependencies:
is-any-array "^2.0.0"
ml-array-max "^1.2.4"
ml-array-min "^1.2.3"

ml-matrix@^6.12.0:
version "6.12.0"
resolved "https://registry.yarnpkg.com/ml-matrix/-/ml-matrix-6.12.0.tgz#def6a0574b5fdc54a753033830e784a17399e270"
integrity sha512-AGfR+pWaC0GmzjUnB6BfwhndPEUGz0i7QUYdqNuw1zhTov/vSRJ9pP2hs6BoGpaSbtXgrKjZz2zjD1M0xuur6A==
dependencies:
is-any-array "^2.0.1"
ml-array-rescale "^1.3.7"

ms@^2.1.3:
version "2.1.3"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
Expand Down Expand Up @@ -2166,6 +2249,11 @@ react-is@^18.0.0:
resolved "https://registry.yarnpkg.com/react-is/-/react-is-18.3.1.tgz#e83557dc12eae63a99e003a46388b1dcbb44db7e"
integrity sha512-/LLMVyas0ljjAtoYiPqYiL8VWXzUUdThrmU5+n20DZv+a+ClRoevUzw5JxU+Ieh5/c87ytoTBV9G1FiKfNJdmg==

regenerator-runtime@^0.14.0:
version "0.14.1"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.14.1.tgz#356ade10263f685dda125100cd862c1db895327f"
integrity sha512-dYnhHh0nJoMfnkZs6GmmhFknAGRrLznOu5nc9ML+EJxGvrx6H7teuevqVqCuPcPK//3eDrrjQhehXVx9cnkGdw==

require-directory@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42"
Expand Down Expand Up @@ -2202,6 +2290,11 @@ resolve@^1.20.0:
path-parse "^1.0.7"
supports-preserve-symlinks-flag "^1.0.0"

seedrandom@^3.0.5:
version "3.0.5"
resolved "https://registry.yarnpkg.com/seedrandom/-/seedrandom-3.0.5.tgz#54edc85c95222525b0c7a6f6b3543d8e0b3aa0a7"
integrity sha512-8OwmbklUNzwezjGInmZ+2clQmExQPvomqjL7LFqOYqtmuxRgQYqOD3mHaU+MvZn5FLUeVxVfQjwLZW/n/JFuqg==

semver@^6.3.0, semver@^6.3.1:
version "6.3.1"
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.1.tgz#556d2ef8689146e46dcea4bfdd095f3434dffcb4"
Expand Down Expand Up @@ -2331,6 +2424,11 @@ test-exclude@^6.0.0:
glob "^7.1.4"
minimatch "^3.0.4"

tiny-emitter@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/tiny-emitter/-/tiny-emitter-2.1.0.tgz#1d1a56edfc51c43e863cbb5382a72330e3555423"
integrity sha512-NB6Dk1A9xgQPMoGqC5CVXn123gWyte215ONT5Pp5a0yt4nlEoO1ZWeCwpncaekPHXO60i47ihFnZPiRPjRMq4Q==

[email protected]:
version "1.0.5"
resolved "https://registry.yarnpkg.com/tmpl/-/tmpl-1.0.5.tgz#8683e0b902bb9c20c4f726e3c0b69f36518c07cc"
Expand Down Expand Up @@ -2397,6 +2495,11 @@ type-fest@^0.21.3:
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.21.3.tgz#d260a24b0198436e133fa26a524a6d65fa3b2e37"
integrity sha512-t0rzBq87m3fVcduHDUFhKmyyX+9eo6WQjZvf51Ea/M0Q7+T374Jp1aUiyUl0GKxp8M/OETVHSDvmkyPgvX+X2w==

typed-function@^4.2.1:
version "4.2.1"
resolved "https://registry.yarnpkg.com/typed-function/-/typed-function-4.2.1.tgz#19aa51847aa2dea9ef5e7fb7641c060179a74426"
integrity sha512-EGjWssW7Tsk4DGfE+5yluuljS1OGYWiI1J6e8puZz9nTMM51Oug8CD5Zo4gWMsOhq5BI+1bF+rWTm4Vbj3ivRA==

typescript@^5.7.2:
version "5.7.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.7.2.tgz#3169cf8c4c8a828cde53ba9ecb3d2b1d5dd67be6"
Expand Down

0 comments on commit c1a3cd2

Please sign in to comment.