-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7_2.java
57 lines (48 loc) · 2.09 KB
/
day7_2.java
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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
class day7_2 extends Extra {
public static void main(String[] aaa) throws Exception {
BufferedReader in = new BufferedReader(new FileReader(new File("inputs/day7")));
String line;
long sum = 0;
while ((line = in.readLine()) != null) {
String[] params = line.split(": ");
long y = Long.parseLong(params[0]);
long[] x = Extra.parseLongs(params[1], " ");
// System.out.println(line);
sum += testOperators(y, x.length - 1, x) ? y : 0;
// System.out.println();
}
System.out.println(sum);
}
public static boolean testOperators(long y, int n, long[] x) {
if (y <= 0) {
return false;
}
if (n == 0) {
if (y == x[0]) {
// System.out.println("Complete");
// System.out.println( y + " " + x[n] + " " + Arrays.toString(x) + " - Valid: "
// + (y == x[0]) + "\n" );
return true;
} else {
return false;
}
} else {
int ydigits = (String.valueOf(y)).length();
int xnDigits = (String.valueOf(x[n])).length();
// System.out.printf( "y: %s, digits: %s; x[n]: %s, digits: %s\n",
// String.valueOf(y), ydigits, x[n], xnDigits );
// if (ydigits > xnDigits) System.out.println( String.valueOf(y).substring(
// ydigits - xnDigits ) );
String deConcat = ydigits > xnDigits
&& Long.parseLong(String.valueOf(y).substring(ydigits - xnDigits)) == x[n]
? String.valueOf(y).substring(0, (ydigits - xnDigits))
: "";
// System.out.println( y + " " + x[n] + " " + deConcat );
return testOperators(y - x[n], n - 1, x) || (y % x[n] == 0 ? testOperators(y / x[n], n - 1, x) : false)
|| (n != 0 && !deConcat.isEmpty() ? testOperators(Long.parseLong(deConcat), n - 1, x) : false);
}
}
}