forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp090.java
More file actions
51 lines (38 loc) · 1.31 KB
/
p090.java
File metadata and controls
51 lines (38 loc) · 1.31 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
/*
* Solution to Project Euler problem 90
* Copyright (c) Project Nayuki. All rights reserved.
*
* https://www.nayuki.io/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
public final class p090 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p090().run());
}
public String run() {
// Each die has (10 choose 6) arrangements, so we have at most 44100 arrangements to check
int count = 0;
for (int i = 0; i < (1 << 10); i++) {
for (int j = i; j < (1 << 10); j++) { // Ensure i <= j to force the dice to be orderless
if (Integer.bitCount(i) == 6 && Integer.bitCount(j) == 6 && isArrangementValid(i, j))
count++;
}
}
return Integer.toString(count);
}
private static int[][] SQUARES = {{0, 1}, {0, 4}, {0, 9}, {1, 6}, {2, 5}, {3, 6}, {4, 9}, {6, 4}, {8, 1}};
private static boolean isArrangementValid(int a, int b) {
if (testBit(a, 6) || testBit(a, 9))
a |= (1 << 6) | (1 << 9);
if (testBit(b, 6) || testBit(b, 9))
b |= (1 << 6) | (1 << 9);
for (int[] sqr : SQUARES) {
if (!(testBit(a, sqr[0]) && testBit(b, sqr[1]) || testBit(a, sqr[1]) && testBit(b, sqr[0])))
return false;
}
return true;
}
private static boolean testBit(int x, int i) {
return ((x >>> i) & 1) != 0;
}
}