-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
148 lines (121 loc) · 2.9 KB
/
index.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import {
INPUT_PATH,
runner,
SAMPLE_PATH,
splitLines,
} from '../../../lib/utils';
const path = `${__dirname}/${INPUT_PATH}`;
const solution = (input: string) => {
const data: number[][][] = [];
let part: number[][] = [];
splitLines(input)
.filter((e) => e.length > 1)
.forEach((row, idx) => {
const [x, y] = row.match(/\d+/g)?.map(Number) || [];
part.push([x, y]);
if ((idx + 1) % 3 === 0) {
data.push(part);
part = [];
}
});
let score = 0;
data.filter(([a, b, prize]) => {
const [ax, ay] = a;
const [bx, by] = b;
const [prizeX, prizeY] = prize;
let result = 0;
for (let i = 0; i <= 100; i++) {
for (let j = 0; j <= 100; j++) {
if (ax * i + bx * j === prizeX && ay * i + by * j === prizeY) {
result = i * 3 + j;
break;
}
}
if (result) {
score += result;
break;
}
}
});
return score;
};
runner({
path,
solution: (input) => solution(input),
});
function extendedGcd(a, b) {
if (b === 0) return { gcd: a, x: 1, y: 0 };
const { gcd, x, y } = extendedGcd(b, a % b);
return {
gcd: gcd,
x: y,
y: x - Math.floor(a / b) * y,
};
}
function findMinTokens(prizeX, prizeY, ax, ay, bx, by) {
// We need to solve:
// i*ax + j*bx = prizeX
// i*ay + j*by = prizeY
const { gcd } = extendedGcd(ax, bx);
if (prizeX % gcd !== 0) return null;
const { gcd: gcd2 } = extendedGcd(ay, by);
if (prizeY % gcd2 !== 0) return null;
// Find a particular solution for the first equation
const g1 = extendedGcd(ax, bx);
const x0 = (g1.x * (prizeX / g1.gcd)) % g1.gcd;
const y0 = (prizeX - ax * x0) / bx;
// Find a particular solution for the second equation
const g2 = extendedGcd(ay, by);
const x1 = (g2.x * (prizeY / g2.gcd)) % g2.gcd;
const y1 = (prizeY - ay * x1) / by;
// Find a common solution
let i = 0;
while (i < 1000000) {
if (i === x0 && i === x1) {
const j = y0 - (ax / g1.gcd) * i;
return { i, j };
}
i++;
}
return null;
}
function solution2(input) {
const lines = input.split('\n');
const data = [];
let part = [];
lines.forEach((row, idx) => {
const [x, y] = row.match(/\d+/g)?.map(Number) || [];
part.push(x, y);
if ((idx + 1) % 3 === 0) {
// Assuming each machine has ax, ay, bx, by, prizeX, prizeY
data.push({
ax: part[0],
ay: part[1],
bx: part[2],
by: part[3],
prizeX: part[4],
prizeY: part[5],
});
part = [];
}
});
let totalScore = 0;
for (const machine of data) {
const result = findMinTokens(
machine.prizeX,
machine.prizeY,
machine.ax,
machine.ay,
machine.bx,
machine.by,
);
if (result !== null) {
totalScore += 3 * result.i + result.j;
}
}
return totalScore;
}
runner({
path,
solution: (input) => solution2(input),
});