-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolynomialSolver.java
More file actions
141 lines (121 loc) · 5.04 KB
/
PolynomialSolver.java
File metadata and controls
141 lines (121 loc) · 5.04 KB
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
import java.io.BufferedReader;
import java.io.FileReader;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
public class PolynomialSolver {
static class Point {
int x;
BigInteger y;
public Point(int x, BigInteger y) {
this.x = x;
this.y = y;
}
}
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java PolynomialSolver <input.json>");
return;
}
try {
// Read and clean JSON file
String json = readAndCleanJsonFile(args[0]);
// Extract keys section
String keysSection = extractJsonSection(json, "keys");
if (keysSection == null) {
System.out.println("Error: Could not find 'keys' section in JSON");
return;
}
// Parse n and k
int n = extractJsonInt(keysSection, "n");
int k = extractJsonInt(keysSection, "k");
// Parse all points
List<Point> points = new ArrayList<>();
int pointCount = 0;
while (pointCount < n) {
pointCount++;
String pointSection = extractJsonSection(json, Integer.toString(pointCount));
if (pointSection == null) continue;
String base = extractJsonString(pointSection, "base");
String value = extractJsonString(pointSection, "value");
BigInteger y = convertToDecimal(value, base);
points.add(new Point(pointCount, y));
}
// Validate
if (points.size() < k) {
System.out.printf("Error: Need %d points but found %d%n", k, points.size());
return;
}
// Calculate
BigInteger C = calculateConstantTerm(points, k);
System.out.println("The secret constant C is: " + C);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
private static String readAndCleanJsonFile(String path) throws Exception {
StringBuilder content = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
String line;
while ((line = reader.readLine()) != null) {
// Remove all whitespace for simpler parsing
content.append(line.replaceAll("\\s", ""));
}
}
return content.toString();
}
private static String extractJsonSection(String json, String key) {
// Pattern to match "key":{...}
String pattern = "\"" + key + "\":\\{([^}]*)\\}";
java.util.regex.Pattern r = java.util.regex.Pattern.compile(pattern);
java.util.regex.Matcher m = r.matcher(json);
return m.find() ? m.group(1) : null;
}
private static int extractJsonInt(String section, String key) throws Exception {
String pattern = "\"" + key + "\":(\\d+)";
java.util.regex.Pattern r = java.util.regex.Pattern.compile(pattern);
java.util.regex.Matcher m = r.matcher(section);
if (m.find()) {
return Integer.parseInt(m.group(1));
}
throw new Exception("Missing or invalid integer value for key: " + key);
}
private static String extractJsonString(String section, String key) throws Exception {
String pattern = "\"" + key + "\":\"([^\"]*)\"";
java.util.regex.Pattern r = java.util.regex.Pattern.compile(pattern);
java.util.regex.Matcher m = r.matcher(section);
if (m.find()) {
return m.group(1);
}
throw new Exception("Missing string value for key: " + key);
}
private static BigInteger convertToDecimal(String value, String baseStr) throws Exception {
BigInteger base = new BigInteger(baseStr);
BigInteger result = BigInteger.ZERO;
String digits = "0123456789abcdef";
for (char c : value.toLowerCase().toCharArray()) {
int digit = digits.indexOf(c);
if (digit < 0 || digit >= base.intValue()) {
throw new Exception("Invalid digit '" + c + "' for base " + base);
}
result = result.multiply(base).add(BigInteger.valueOf(digit));
}
return result;
}
private static BigInteger calculateConstantTerm(List<Point> points, int k) {
BigInteger C = BigInteger.ZERO;
for (int i = 0; i < k; i++) {
BigInteger term = points.get(i).y;
for (int j = 0; j < k; j++) {
if (i != j) {
BigInteger xi = BigInteger.valueOf(points.get(i).x);
BigInteger xj = BigInteger.valueOf(points.get(j).x);
term = term.multiply(xj.negate())
.divide(xi.subtract(xj));
}
}
C = C.add(term);
}
return C;
}
}