Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 25555661
- import java.io.*;
- import java.util.*;
- public class Domino {
- public static void main(String[] args) throws FileNotFoundException
- {
- Scanner in = new Scanner(new File("domino.dat"));
- String domino = in.nextLine();
- char frontChar = domino.charAt(0); // first number of the layout
- char backChar = domino.charAt(domino.length() - 1);// last number of the layout
- while(in.hasNextLine()) {
- String line = in.nextLine();
- Scanner sc = new Scanner(line);
- ArrayList<Integer> sorter = new ArrayList<>();// to sorter for the values
- HashMap<Integer,Boolean> reverser = new HashMap<>(); // In order to determine whether to reverse the domino or not
- for(int i = 0; i < 4; i++ ) {
- String s = sc.next(); // The next domino
- if(s.indexOf(frontChar) == 1 || s.indexOf(backChar) == 0) { // if the domino is valid and does not need to be reversed
- sorter.add(Integer.parseInt(s)); // add the integer value
- 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'
- }else if(s.indexOf(frontChar) == 0 || s.indexOf(backChar) == 1) { // if the domino is valid and needs to be reversed
- StringBuilder cc = new StringBuilder(s);
- cc.reverse();
- s = cc.toString(); // reversed string
- sorter.add(Integer.parseInt(s)); // add the integer value reversed(we will reverse it back later)
- 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'
- }
- }
- if(sorter.isEmpty()) { // if theere are no valid dominoes
- System.out.println("none");
- }else {
- Collections.sort(sorter); // sort the values
- Integer x= sorter.get(sorter.size() - 1); // value of largest integer
- StringBuilder s = new StringBuilder("" + x);
- s.reverse();// reversesd string of original integer
- 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)
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment