Skip to content

Commit

Permalink
Merge pull request #12 from dieseltravis/day13
Browse files Browse the repository at this point in the history
Day 13
  • Loading branch information
dieseltravis authored Dec 14, 2024
2 parents 961eddf + d741d29 commit 613ff92
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "meowing-holy-carbon",
"version": "2024.12.12",
"version": "2024.12.13",
"description": "Travis's Advent of Code 2024",
"author": "Travis Hardiman",
"homepage": "https://github.com/dieseltravis/aoc2024/",
Expand Down
124 changes: 122 additions & 2 deletions public/funs.js
Original file line number Diff line number Diff line change
Expand Up @@ -993,8 +993,128 @@
}
},
day13: {
part1: d => d,
part2: d => d
part1: (data) => {
const matchButtons = /X\+(\d+), Y\+(\d+)/;
const matchPrize = /X=(\d+), Y=(\d+)/;
let sum = 0;
const input = data.trim().split('\n\n').map(r => {
const claw = r.split('\n');
const buttonA = claw[0].match(matchButtons);
const buttonB = claw[1].match(matchButtons);
const prize = claw[2].match(matchPrize);
const game = {
a: {
x: +buttonA[1],
y: +buttonA[2]
},
b: {
x: +buttonB[1],
y: +buttonB[2]
},
p: {
x: +prize[1],
y: +prize[2]
}
};
// start at max B value
for (let b = 100; b--;) {
const bx = b * game.b.x;
if (bx <= game.p.x) {
const by = b * game.b.y;
if (by <= game.p.y) {
// remainder after B value
const rx = game.p.x - bx;
if (rx % game.a.x === 0) {
const ry = game.p.y - by;
if (ry % game.a.y === 0) {
// check A values are equal
const axv = rx / game.a.x;
const ayv = ry / game.a.y;
if (axv === ayv) {
game.p.b = b;
game.p.a = axv;
game.p.t = (axv * 3) + b;
sum += game.p.t;
break;
}
}
}
}
}
}
return game;
});
console.log(input, sum);
return sum;
},
part2: (data) => {
const matchButtons = /X\+(\d+), Y\+(\d+)/;
const matchPrize = /X=(\d+), Y=(\d+)/;
const input = data.trim().split('\n\n').map(r => {
const claw = r.split('\n');
const buttonA = claw[0].match(matchButtons);
const buttonB = claw[1].match(matchButtons);
const prize = claw[2].match(matchPrize);
const game = {
a: {
x: +buttonA[1],
y: +buttonA[2]
},
b: {
x: +buttonB[1],
y: +buttonB[2]
},
p: {
x: +prize[1] + 10000000000000,
y: +prize[2] + 10000000000000
}
};
return game;
});
const ymax = input.length;
console.log(input, ymax);
const sum = input.reduce((acc, game, i) => {
// start at max B value
const bmax = Math.floor(Math.min(game.p.x / game.b.x, game.p.y / game.b.y)) + 1;
const percentage = bmax / 100;
let last = bmax;
let percent = 100;
console.log('processing ' + (i + 1) + ' of ' + ymax + ', counting down from ' + bmax);
for (let b = bmax; b--;) {
const bx = b * game.b.x;
if (bx <= game.p.x) {
const by = b * game.b.y;
if (by <= game.p.y) {
// remainder after B value
const rx = game.p.x - bx;
if (rx % game.a.x === 0) {
const ry = game.p.y - by;
if (ry % game.a.y === 0) {
// check A values are equal
const axv = rx / game.a.x;
const ayv = ry / game.a.y;
if (axv === ayv) {
game.p.b = b;
game.p.a = axv;
game.p.t = (axv * 3) + b;
acc += game.p.t;
console.log(acc, game.p.t);
break;
}
}
}
}
}
if (b < last) {
console.log((100 - percent) + '%');
last -= percentage;
percent--;
}
}
return acc;
}, 0);
return sum;
}
},
day14: {
part1: d => d,
Expand Down
2 changes: 1 addition & 1 deletion views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ <h2>solutions:</h2>
<li><a href="/day/10">day 10</a></li>
<li><a href="/day/11">day 11</a></li>
<li><a href="/day/12">day 12</a></li>
<li><a href="/day/13">day 13</a> (part 2 very slow)</li>
<!--
<li><a href="/day/13">day 13</a></li>
<li><a href="/day/14">day 14</a></li>
<li><a href="/day/15">day 15</a></li>
<li><a href="/day/16">day 16</a></li>
Expand Down

0 comments on commit 613ff92

Please sign in to comment.