-
Notifications
You must be signed in to change notification settings - Fork 0
/
part2.ts
31 lines (27 loc) · 1.21 KB
/
part2.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import * as fs from 'fs';
const input = fs.readFileSync('input', 'utf8').split('\n\n').map((block => {
const [rawA, rawB, rawPrize] = block.split('\n');
const [, xA, yA] = rawA.match(/X\+(\d+), Y\+(\d+)/);
const [, xB, yB] = rawB.match(/X\+(\d+), Y\+(\d+)/);
const [, xPrize, yPrize] = rawPrize.match(/X=(\d+), Y=(\d+)/);
return { xA: Number(xA), yA: Number(yA), xB: Number(xB), yB: Number(yB), xPrize: Number(xPrize) + 10000000000000, yPrize: Number(yPrize) + 10000000000000 };
}));
// xPrize = countA * xA + countB * xB
// yPrize = countA * yA + countB * yB
// --
// countB = (xPrize - countA * xA) / xB
// --
// yPrize = countA * yA + (xPrize - countA * xA) / xB * yB
// yPrize = countA * yA + xPrize * yB / xB - countA * xA * yB / xB
// countA = (yPrize - xPrize * yB / xB) / (yA - xA * yB / xB)
// countA = (yPrize * xB - xPrize * yB) / (yA * xB - xA * yB)
const solve = ({ xA, yA, xB, yB, xPrize, yPrize }) => {
const countA = (yPrize * xB - xPrize * yB) / (yA * xB - xA * yB);
const countB = (xPrize - countA * xA) / xB;
if (Math.floor(countA) === countA && Math.floor(countB) === countB) {
return countA * 3 + countB;
}
return 0;
}
let result = input.map(solve).reduce((a, b) => a + b, 0);
console.log(result);