-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindSecret.js
53 lines (44 loc) · 1.28 KB
/
findSecret.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const fs = require('fs');
function decodeBase(value, base) {
return parseInt(value, base);
}
function parseInput(filename) {
const data = JSON.parse(fs.readFileSync(filename, 'utf-8'));
const keys = data.keys;
const points = [];
for (const key in data) {
if (key !== 'keys') {
const x = parseInt(key, 10);
const base = parseInt(data[key].base, 10);
const y = decodeBase(data[key].value, base);
points.push({ x, y });
}
}
return { keys, points };
}
function lagrangeInterpolation(points, k) {
let constantTerm = 0;
for (let i = 0; i < k; i++) {
let xi = points[i].x;
let yi = points[i].y;
let term = yi;
for (let j = 0; j < k; j++) {
if (i !== j) {
let xj = points[j].x;
term *= xj / (xj - xi);
}
}
constantTerm += term;
}
return Math.round(constantTerm);
}
function findSecret(filename) {
const { keys, points } = parseInput(filename);
const { k } = keys;
const secret = lagrangeInterpolation(points, k);
console.log(`Secret (constant term c): ${secret}`);
}
const filename1 = 'testcase1.json';
const filename2 = 'testcase2.json';
findSecret(filename1);
findSecret(filename2);