public class Puzzle {
/** Solve the word puzzle.
* @param args three words (addend1, addend2 and sum)
*/
public static void main(String[] args) {
// args = new String[]{"YKS", "KAKS", "KOLM"}; // 234 solutions
args = new String[]{"SEND", "MORE", "MONEY"}; // 1 solution
// args = new String[]{"ABCDEFGHIJAB", "ABCDEFGHIJA", "ACEHJBDFGIAC"}; // 2 solutions
// {A=1, B=2, C=3, D=4, E=5, F=6, G=7, H=8, I=9, J=0},
// {A=2, B=3, C=5, D=1, E=8, F=4, G=6, H=7, I=9, J=0}
// args = new String[]{"CBEHEIDGEI", "CBEHEIDGEI", "BBBBBBBBBB"}; // no solutions
// args = new String[]{"aBCDEFGHIJAB", "ABCDEFGHIJA", "ACEHJBDFGIAC"}; // We can only use capital letters (addend1)
// args = new String[]{"ABCDEFGHIJAB", "ABcdeFGHIJA", "ACEHJBDFGIAC"}; // We can only use capital letters (addend2)
// args = new String[]{"ABCDEFGHIJAB", "ABcdeFGHIJA", "acehjbdfgiac"}; // We can only use capital letters (sum)
// args = new String[]{"ABCDEFGHIJAB", "", ""}; // We can only use capital letters (addend2 + sum)
String addend1 = args[0];
String addend2 = args[1];
String sum = args[2];
if (addend1.matches("[A-Z]+")) {
if (!addend2.matches("[A-Z]+")) {
throw new RuntimeException("We can only use capital letters");
}
if (!sum.matches("[A-Z]+")) {
throw new RuntimeException("We can only use capital letters");
}
Map<Character, Integer> position;
position = placeMap(args);
//https://stackoverflow.com/questions/29663704/using-hashmap-to-assign-val-to-char
int[] arrayList; //Используется для замены букв цифрами
arrayList = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; //Максимум можем использовать 10 цифр
Set<String> results;
results = new HashSet<>(); //убираем дупликат
if (replacement(arrayList, args, position)) {
results.add(addSymbol(position, arrayList));
}
while (arDropping(arrayList))
if (replacement(arrayList, args, position)) {
results.add(addSymbol(position, arrayList));
}
if (results.size() == 1) {
System.out.println(addend1 + " + " + addend2 + " = " + sum);
System.out.println(results.size() + " solution:"); //количество решений для 1
for (String result : results) {
System.out.println(result); //Самоо решение
}
}else if (results.size() > 1) {
System.out.println(addend1 + " + " + addend2 + " = " + sum);
System.out.println(results.size() + " solutions:"); //количество решений для 2 и более
for (String result : results) {
System.out.println(result); //Самоо решение
}
} else {
System.out.println("No solutions");
}
} else {
throw new RuntimeException("We can only use capital letters");
}
}
private static boolean arDropping(int[] arrayList) {
int el1Indx = -1, el2Indx = -1;
{
int i = arrayList.length - 2;
while (i >= 0) {
if (arrayList[i] < arrayList[i + 1]) {
el1Indx = i;
break;
}
i--;
}
}
if (el1Indx == -1) {
return false;
}
int i = arrayList.length - 1;
while (i >= el1Indx) {
if (arrayList[i] > arrayList[el1Indx]) {
el2Indx = i;
break;
}
i--;
}
if (el2Indx == -1) {
return false;
}
shift(arrayList, el1Indx, el2Indx);
reverse(arrayList, el1Indx);
return true;
}
private static boolean replacement(int[] arrayList, String[] input, Map<Character, Integer> position) {
String firstBlock; //Меняем значения
firstBlock = otherPosition(input[0], position, arrayList);
String secondBlock;
secondBlock = otherPosition(input[1], position, arrayList);
String sum;
sum = otherPosition(input[2], position, arrayList);
if (firstBlock.charAt(0) == '0') {[![введите сюда описание изображения][1]][1]
return false;
} else if (secondBlock.charAt(0) == '0') {
return false;
} else if (sum.charAt(0) == '0') {
return false;
}
long first;
first = Long.parseLong(firstBlock);
long second;
second = Long.parseLong(secondBlock);
long summa;
summa = Long.parseLong(sum);
long longSum = first + second;
return summa == longSum;
}
private static String addSymbol(Map<Character, Integer> position, int[] arrayList) {
StringBuilder sb;
sb = new StringBuilder();
sb.append("{");
for (Map.Entry<Character, Integer> entry : position.entrySet()) {
sb.append(entry.getKey()).append('=').append(arrayList[entry.getValue()]).append(", ");
}
sb.delete(sb.length() - 2, sb.length()).append("}");
return sb.toString();
}
private static String otherPosition(String input, Map<Character, Integer> position, int[] arrayList) {
StringBuilder sb;
sb = new StringBuilder();
int i = 0;
while (i < input.length()) {
sb.append(arrayList[position.get(input.charAt(i))]);
i++;
}
return sb.toString();
}
private static Map<Character, Integer> placeMap(String[] input) {
String word;
word = input[0] + input[1] + input[2];
Set<Character> chars;
chars = new HashSet<>();//Избегаем повторения
for (char c : word.toCharArray()) {
chars.add(c);
}
Map<Character, Integer> letter;
letter = new HashMap<>();
int i = 0;
for (Character c : chars) {
letter.put(c, i++);
}
return letter;
}
private static void reverse(int[] massiv, int index) {
int i;
for (i = index + 1; ((massiv.length - index) / 2) + index >= i; i++) {
int j;
j = massiv.length - (i - index);
shift(massiv, i, j);
}
}
private static void shift(int[] massiv, int el1Indx, int el2Indx) {
int re = massiv[el1Indx];
massiv[el1Indx] = massiv[el2Indx];
massiv[el2Indx] = re;
}
}```