Advertisement
UniQuet0p1

Untitled

Dec 2nd, 2021
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.08 KB | None | 0 0
  1. //https://stackoverflow.com/questions/13216314/hashmap-does-not-work-with-int-char/13216330
  2. //https://stackoverflow.com/questions/13595497/creating-an-unordered-map-of-char-int-in-java
  3. //https://www.tabnine.com/code/java/methods/java.util.stream.IntStream/iterate
  4. //https://stackoverflow.com/questions/23981008/how-to-replace-existing-value-of-arraylist-element-in-java
  5. //https://docs.oracle.com/javase/tutorial/java/data/buffers.html
  6. //https://www.tabnine.com/code/java/methods/java.util.TreeMap/entrySet
  7. //https://www.tabnine.com/code/java/methods/java.lang.StringBuilder/delete
  8. //https://www.delftstack.com/howto/java/shift-java-array/
  9. //https://stackoverflow.com/questions/2137755/how-do-i-reverse-an-int-array-in-java/34172032#34172032
  10. //https://beginnersbook.com/2013/12/java-string-tochararray-method-example/
  11.  
  12. import java.util.*;
  13.  
  14.  
  15. public class Puzzle {
  16. /** Solve the word puzzle.
  17. * @param args three words (addend1, addend2 and sum)
  18. */
  19. public static void main(String[] args) {
  20.  
  21. // args = new String[]{"YKS", "KAKS", "KOLM"}; // 234 solutions
  22. args = new String[]{"SEND", "MORE", "MONEY"}; // 1 solution
  23. // args = new String[]{"ABCDEFGHIJAB", "ABCDEFGHIJA", "ACEHJBDFGIAC"}; // 2 solutions
  24. // {A=1, B=2, C=3, D=4, E=5, F=6, G=7, H=8, I=9, J=0},
  25. // {A=2, B=3, C=5, D=1, E=8, F=4, G=6, H=7, I=9, J=0}
  26. // args = new String[]{"CBEHEIDGEI", "CBEHEIDGEI", "BBBBBBBBBB"}; // no solutions
  27. // args = new String[]{"aBCDEFGHIJAB", "ABCDEFGHIJA", "ACEHJBDFGIAC"}; // We can only use capital letters (addend1)
  28. // args = new String[]{"ABCDEFGHIJAB", "ABcdeFGHIJA", "ACEHJBDFGIAC"}; // We can only use capital letters (addend2)
  29. // args = new String[]{"ABCDEFGHIJAB", "ABcdeFGHIJA", "acehjbdfgiac"}; // We can only use capital letters (sum)
  30. // args = new String[]{"ABCDEFGHIJAB", "", ""}; // We can only use capital letters (addend2 + sum)
  31.  
  32.  
  33. String addend1 = args[0];
  34. String addend2 = args[1];
  35. String sum = args[2];
  36.  
  37. if (addend1.matches("[A-Z]+")) {
  38. if (!addend2.matches("[A-Z]+")) {
  39. throw new RuntimeException("We can only use capital letters");
  40. }
  41. if (!sum.matches("[A-Z]+")) {
  42. throw new RuntimeException("We can only use capital letters");
  43. }
  44.  
  45.  
  46. Map<Character, Integer> position;
  47. position = placeMap(args);
  48. //https://stackoverflow.com/questions/29663704/using-hashmap-to-assign-val-to-char
  49. int[] arrayList; //Используется для замены букв цифрами
  50. arrayList = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; //Максимум можем использовать 10 цифр
  51. Set<String> results;
  52. results = new HashSet<>(); //убираем дупликат
  53.  
  54.  
  55. if (replacement(arrayList, args, position)) {
  56. results.add(addSymbol(position, arrayList));
  57. }
  58. while (arDropping(arrayList))
  59. if (replacement(arrayList, args, position)) {
  60. results.add(addSymbol(position, arrayList));
  61. }
  62. if (results.size() == 1) {
  63. System.out.println(addend1 + " + " + addend2 + " = " + sum);
  64. System.out.println(results.size() + " solution:"); //количество решений для 1
  65. for (String result : results) {
  66. System.out.println(result); //Самоо решение
  67. }
  68. }else if (results.size() > 1) {
  69. System.out.println(addend1 + " + " + addend2 + " = " + sum);
  70. System.out.println(results.size() + " solutions:"); //количество решений для 2 и более
  71. for (String result : results) {
  72. System.out.println(result); //Самоо решение
  73. }
  74. } else {
  75. System.out.println("No solutions");
  76. }
  77. } else {
  78. throw new RuntimeException("We can only use capital letters");
  79. }
  80. }
  81.  
  82. private static boolean arDropping(int[] arrayList) {
  83. int el1Indx = -1, el2Indx = -1;
  84. {
  85. int i = arrayList.length - 2;
  86. while (i >= 0) {
  87. if (arrayList[i] < arrayList[i + 1]) {
  88. el1Indx = i;
  89. break;
  90. }
  91. i--;
  92. }
  93. }
  94. if (el1Indx == -1) {
  95. return false;
  96. }
  97. int i = arrayList.length - 1;
  98. while (i >= el1Indx) {
  99. if (arrayList[i] > arrayList[el1Indx]) {
  100. el2Indx = i;
  101. break;
  102. }
  103. i--;
  104. }
  105. if (el2Indx == -1) {
  106. return false;
  107. }
  108. shift(arrayList, el1Indx, el2Indx);
  109. reverse(arrayList, el1Indx);
  110. return true;
  111. }
  112.  
  113.  
  114. private static boolean replacement(int[] arrayList, String[] input, Map<Character, Integer> position) {
  115. String firstBlock; //Меняем значения
  116. firstBlock = otherPosition(input[0], position, arrayList);
  117.  
  118. String secondBlock;
  119. secondBlock = otherPosition(input[1], position, arrayList);
  120.  
  121. String sum;
  122. sum = otherPosition(input[2], position, arrayList);
  123.  
  124. if (firstBlock.charAt(0) == '0') { //Zero can not be first symbol
  125. return false;
  126. } else if (secondBlock.charAt(0) == '0') {
  127. return false;
  128. } else if (sum.charAt(0) == '0') {
  129. return false;
  130. }
  131.  
  132.  
  133.  
  134. long first;
  135. first = Long.parseLong(firstBlock);
  136.  
  137.  
  138. long second;
  139. second = Long.parseLong(secondBlock);
  140.  
  141. long summa;
  142. summa = Long.parseLong(sum);
  143.  
  144. long longSum = first + second;
  145. return summa == longSum;
  146. }
  147.  
  148.  
  149.  
  150. private static String addSymbol(Map<Character, Integer> position, int[] arrayList) { //https://www.tabnine.com/code/java/methods/java.util.TreeMap/entrySet
  151. StringBuilder sb;
  152. sb = new StringBuilder();
  153. sb.append("{");
  154.  
  155. for (Map.Entry<Character, Integer> entry : position.entrySet()) {
  156. sb.append(entry.getKey()).append('=').append(arrayList[entry.getValue()]).append(", ");
  157. }
  158. //https://www.tabnine.com/code/java/methods/java.lang.StringBuilder/delete
  159. sb.delete(sb.length() - 2, sb.length()).append("}");
  160. return sb.toString();
  161.  
  162. }
  163.  
  164.  
  165.  
  166. private static String otherPosition(String input, Map<Character, Integer> position, int[] arrayList) {
  167. StringBuilder sb;
  168. sb = new StringBuilder();
  169. int i = 0;
  170. while (i < input.length()) {
  171. sb.append(arrayList[position.get(input.charAt(i))]);
  172. i++;
  173. }
  174. return sb.toString();
  175. }
  176.  
  177.  
  178. private static Map<Character, Integer> placeMap(String[] input) {
  179.  
  180.  
  181. String word;
  182. word = input[0] + input[1] + input[2];
  183.  
  184.  
  185. Set<Character> chars;
  186. chars = new HashSet<>();//Избегаем повторения
  187.  
  188. for (char c : word.toCharArray()) { //https://beginnersbook.com/2013/12/java-string-tochararray-method-example/
  189. chars.add(c);
  190.  
  191. }
  192. Map<Character, Integer> letter;
  193. letter = new HashMap<>();
  194. int i = 0;
  195.  
  196. for (Character c : chars) {
  197. letter.put(c, i++);
  198. }
  199. return letter;
  200. }
  201.  
  202.  
  203. private static void reverse(int[] massiv, int index) { //https://stackoverflow.com/questions/2137755/how-do-i-reverse-an-int-array-in-java/34172032#34172032
  204. int i;
  205. for (i = index + 1; ((massiv.length - index) / 2) + index >= i; i++) {
  206. int j;
  207. j = massiv.length - (i - index);
  208. shift(massiv, i, j);
  209. }
  210. }
  211.  
  212.  
  213. private static void shift(int[] massiv, int el1Indx, int el2Indx) { //https://www.delftstack.com/howto/java/shift-java-array/
  214. int re = massiv[el1Indx];
  215. massiv[el1Indx] = massiv[el2Indx];
  216. massiv[el2Indx] = re;
  217. }
  218. }
  219.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement