Guest User

Untitled

a guest
Nov 20th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. 25555661
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. public class Domino {
  6. public static void main(String[] args) throws FileNotFoundException
  7. {
  8. Scanner in = new Scanner(new File("domino.dat"));
  9. String domino = in.nextLine();
  10. char frontChar = domino.charAt(0); // first number of the layout
  11. char backChar = domino.charAt(domino.length() - 1);// last number of the layout
  12. while(in.hasNextLine()) {
  13. String line = in.nextLine();
  14. Scanner sc = new Scanner(line);
  15.  
  16. ArrayList<Integer> sorter = new ArrayList<>();// to sorter for the values
  17. HashMap<Integer,Boolean> reverser = new HashMap<>(); // In order to determine whether to reverse the domino or not
  18.  
  19. for(int i = 0; i < 4; i++ ) {
  20. String s = sc.next(); // The next domino
  21. if(s.indexOf(frontChar) == 1 || s.indexOf(backChar) == 0) { // if the domino is valid and does not need to be reversed
  22. sorter.add(Integer.parseInt(s)); // add the integer value
  23. reverser.put(Integer.parseInt(s),false); // add the integer value as the key and say that it does not need to be reversed by putting 'false'
  24. }else if(s.indexOf(frontChar) == 0 || s.indexOf(backChar) == 1) { // if the domino is valid and needs to be reversed
  25. StringBuilder cc = new StringBuilder(s);
  26. cc.reverse();
  27. s = cc.toString(); // reversed string
  28. sorter.add(Integer.parseInt(s)); // add the integer value reversed(we will reverse it back later)
  29. reverser.put(Integer.parseInt(s),true); // add the integer value as the key and say that it does not need to be reversed by putting 'false'
  30. }
  31. }
  32. if(sorter.isEmpty()) { // if theere are no valid dominoes
  33. System.out.println("none");
  34. }else {
  35. Collections.sort(sorter); // sort the values
  36. Integer x= sorter.get(sorter.size() - 1); // value of largest integer
  37. StringBuilder s = new StringBuilder("" + x);
  38. s.reverse();// reversesd string of original integer
  39. System.out.println((reverser.get(x).equals(false)?x:s.toString())); // if the boolean value is false print the integer otherwise print the reversed value(which is the orignal one in the input file)
  40. }
  41.  
  42.  
  43. }
  44. }
  45.  
  46. }
Add Comment
Please, Sign In to add comment