-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13b.js
37 lines (29 loc) · 871 Bytes
/
13b.js
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
32
33
34
35
36
37
import {readInput} from "./lib.js";
const input = readInput(13);
const machines = []
for (let i = 0; i < input.length; i += 4) {
const a = input[i].match(/(\d+)/g);
const b = input[i + 1].match(/(\d+)/g);
const prize = input[i + 2].match(/(\d+)/g);
machines.push({
a: [parseInt(a[0]), parseInt(a[1])],
b: [parseInt(b[0]), parseInt(b[1])],
prize: [
10000000000000 + parseInt(prize[0]),
10000000000000 + parseInt(prize[1]),
]
})
}
let total = 0;
for (const {a, b, prize} of machines) {
const [ax, ay] = a;
const [bx, by] = b;
const [x, y] = prize;
const A = (bx*y - by*x) / (bx*ay - by*ax)
const B = (x-ax*A) / bx
if (Math.abs(A - Math.round(A)) > 0.0001 || Math.abs(B - Math.round(B)) > 0.0001) {
continue;
}
total += 3*A + B
}
console.log(total);