There are a number of dice, and the input array contains the number on the dice's face up. Dice is 6 faced. Calculate the total number of minimum rotations of dice, to make all faces the same. 1 will require only one rotation to have 2, 3, 4 and 5 face up, but would require minimum two rotations to make it the face 6, as 6 is the opposite side of 1. The opposite side of 2 is 5 and 3 is 4.
I have come up with a solution, but I believe there should be a better solution.
For example:
A = {1,1,6}
, Answer = 2. Rotate 6 two times to get 1.A = {1,2,3}
, Answer = 2. Rotate 1 and 2 and make them 3.A = {1,6,2,3}
, Answer = 3. Rotate 1, 6 and 3 to make them all 2.
import java.util.*;
public class DiceProblem {
public static void main(String args[]){
int[] A = {3,4,1,2,4,2,3,5,1,2,3,4,6,2,4,1,5,2};
Map<Integer, Integer> countMap = new HashMap<>();
int rotation = 0;
int diceCount;
int maxDiceNumber = A[0];
int OppositeOfMaxDiceNumber;
int max = 1;
for(int i = 1; i <= 6 ; i++){
diceCount = 0;
for (int value : A) {
if(i == value){
diceCount++;
}
}
countMap.put(i, diceCount);
if(diceCount > max){
max = diceCount;
maxDiceNumber = i;
}
}
if(max == 1){
if(countMap.get(1).equals(countMap.get(6)) && countMap.get(1) != 0 && countMap.get(2) != 0){
maxDiceNumber = 2;
}else if(countMap.get(2).equals(countMap.get(5)) && countMap.get(2) != 0 && countMap.get(3) != 0){
maxDiceNumber = 3;
}else if(countMap.get(3).equals(countMap.get(4)) && countMap.get(1) != 0){
maxDiceNumber = 1;
}else if(countMap.get(2) != 0){
maxDiceNumber = 2;
}else if(countMap.get(5) != 0){
maxDiceNumber = 5;
}else if(countMap.get(6) != 0){
maxDiceNumber = 6;
}
}
System.out.println("Max Dice Number: "+ maxDiceNumber);
OppositeOfMaxDiceNumber = createOpposite(maxDiceNumber);
System.out.println("Opposite Dice Number: "+ OppositeOfMaxDiceNumber);
Iterator it2 = countMap.entrySet().iterator();
while (it2.hasNext()) {
Map.Entry pair = (Map.Entry)it2.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
if((int)(pair.getValue()) > 0 && (int)(pair.getKey()) != maxDiceNumber){
if((int)(pair.getKey()) == OppositeOfMaxDiceNumber){
rotation = rotation + (2 * (int)(pair.getValue()));
}else {
rotation = rotation + ((int)(pair.getValue()));
}
}
it2.remove(); // avoids a ConcurrentModificationException
}
System.out.println("Number of Minimum Rotations: "+ rotation);
}
private static int createOpposite(int key){
switch (key) {
case 1:
return 6;
case 2:
return 5;
case 3:
return 4;
case 4:
return 3;
case 5:
return 2;
case 6:
return 1;
}
return 0;
}}
public class DiceProblem {
public static void main(String args[]){
int[] A = {3,4,1,2,4,2,3,5,1,2,3,4,6,2,4,1,5,2};
int flip_count;
int min_flip_count = 9999999;
for (int value : A) {
flip_count = 0;
for (int i : A) {
if (value == i) {
flip_count += 0;
} else if (value + i == 7) {
flip_count += 2;
} else {
flip_count += 1;
}
}
if (flip_count < min_flip_count) {
min_flip_count = flip_count;
}
}
System.out.println("Minimum Flip Count:" + min_flip_count);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With