Untitled

mail@pastecode.io avatar
unknown
java
3 years ago
11 kB
3
Indexable
Never
import java.util.*;

public class MarpleSolver {

    enum ClueType {
        BETWEEN, SAME, LEFTOF, NEXTTO;
    }

    public static String solve(String[] clues){

        String[] AbcArray = { "NONE", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

        ArrayList <Letter> Letters = new ArrayList <Letter>();

        int IndexNummberForLetter = 0;
        for(String letter:AbcArray){
            Letters.add(new Letter(letter, IndexNummberForLetter));
            IndexNummberForLetter++;
        }


        ArrayList <Clue> Clues = new ArrayList <Clue>();

        for(String c:clues){

            String[] clue;
            clue = c.split("(?!^)");

            if (clue[1].matches("<")) {
                Clues.add(new Clue(ClueType.LEFTOF, clue));
                continue;
            }

            if (clue[1].matches("\\^")){
                Clues.add(new Clue(ClueType.SAME, clue));
                continue;
            }

            if (clue[1].matches("[A-Z]")){
                if (clue[0].matches(clue[2])){
                    Clues.add(new Clue(ClueType.NEXTTO, clue));
                } else {
                    Clues.add(new Clue(ClueType.BETWEEN, clue));
                }
            }

        }

        for(Clue c:Clues){
            if(c.ClueType == ClueType.LEFTOF){
                Letters.get(c.IntClueValue[0]).viableColumns[4] = false;
                Letters.get(c.IntClueValue[2]).viableColumns[0] = false;
            }
            if(c.ClueType == ClueType.BETWEEN){
                Letters.get(c.IntClueValue[1]).viableColumns[0] = false;
                Letters.get(c.IntClueValue[1]).viableColumns[4] = false;
            }
            if(c.ClueType == ClueType.SAME){
                for(int i = 0; i < 5;i++) {
                    if (Letters.get(c.IntClueValue[0]).viableColumns[i] == false){
                        Letters.get(c.IntClueValue[2]).viableColumns[i] = false;
                    }
                    if (Letters.get(c.IntClueValue[2]).viableColumns[i] == false){
                        Letters.get(c.IntClueValue[0]).viableColumns[i] = false;
                    }
                }
            }
          if(c.ClueType == ClueType.NEXTTO){
              Letters.get(c.IntClueValue[1]).viableColumns[0] = false;
              Letters.get(c.IntClueValue[1]).viableColumns[4] = false;
          }
        }

        for(int count = 0; count <30; count++) {
            for (Clue c : Clues) {
                if (c.ClueType == ClueType.BETWEEN) {
                    for (int i = 0; i < 5; i++) {
                        for (int b = 0; b < 5; b++) {
                            if (Letters.get(c.IntClueValue[0]).viableColumns[i] == false && Letters.get(c.IntClueValue[2]).viableColumns[i] == false && Letters.get(c.IntClueValue[0]).viableColumns[b] == false && Letters.get(c.IntClueValue[2]).viableColumns[b] == false) {
                                boolean first = true;
                                for (int z = 0; z < 5; z++) {
                                    if (z == i || z == b) {
                                        continue;
                                    }
                                    if (first) {
                                        first = false;
                                        continue;
                                    }
                                    Letters.get(c.IntClueValue[1]).setViableColumn(z);
                                    break;
                                }
                            }
                        }
                    }

                }
                if(c.ClueType == ClueType.LEFTOF){
                    int pos0 = Letters.get(c.IntClueValue[0]).isSet();
                    if(pos0 < 5){
                        for(; pos0 >= 0; pos0--){
                            Letters.get(c.IntClueValue[2]).viableColumns[pos0] = false;
                        }
                    }
                    int pos2 = Letters.get(c.IntClueValue[2]).isSet();
                    if(pos2 < 5){
                        for(; pos2 < 5; pos2++){
                            Letters.get(c.IntClueValue[0]).viableColumns[pos2] = false;
                        }
                    }
                }
                if(c.ClueType == ClueType.NEXTTO){
                    if(Letters.get(c.IntClueValue[1]).viableColumns[1] == false){
                        Letters.get(c.IntClueValue[0]).viableColumns[0] = false;
                    }
                    if(Letters.get(c.IntClueValue[1]).viableColumns[3] == false){
                        Letters.get(c.IntClueValue[0]).viableColumns[4] = false;
                    }
                    if(Letters.get(c.IntClueValue[1]).viableColumns[2] == false){
                      if(Letters.get(c.IntClueValue[1]).viableColumns[1] == false){
                          Letters.get(c.IntClueValue[0]).viableColumns[1] = false;
                      }
                      if(Letters.get(c.IntClueValue[1]).viableColumns[3] == false){
                          Letters.get(c.IntClueValue[0]).viableColumns[3] = false;
                      }                       
                    }
                    if(Letters.get(c.IntClueValue[0]).viableColumns[1] == false){
                        Letters.get(c.IntClueValue[1]).viableColumns[0] = false;
                    }
                    if(Letters.get(c.IntClueValue[0]).viableColumns[3] == false){
                        Letters.get(c.IntClueValue[1]).viableColumns[4] = false;
                    }
                    if(Letters.get(c.IntClueValue[0]).viableColumns[2] == false){
                      if(Letters.get(c.IntClueValue[0]).viableColumns[1] == false){
                          Letters.get(c.IntClueValue[1]).viableColumns[1] = false;
                      }
                      if(Letters.get(c.IntClueValue[0]).viableColumns[3] == false){
                          Letters.get(c.IntClueValue[1]).viableColumns[3] = false;
                      }                       
                    }
                }
            }
        }
        
        for(int y = 0; y < 30; y++){
          for(int x = 0; x < 30; x++){
            for(Letter l:Letters){
              int isSet = l.isSet();
              if(isSet == x && isSet == y)
                System.out.println(l.Name);
            }
          }
        }

        return "";

    }

    public static class Letter{

        public int IndexNummber;

        public String Name;

        public int Row;

        public boolean[] viableColumns = {true, true, true, true, true};

        public Letter(String letter, int index){

            IndexNummber = index;

            Name = letter;

            if(letter.matches("[A-E]")){
                Row = 0;
            }
            if(letter.matches("[F-J]")){
                Row = 1;
            }
            if(letter.matches("[K-O]")){
                Row = 2;
            }
            if(letter.matches("[P-T]")){
                Row = 3;
            }
        }

        public void setViableColumn(int index) {
            for(int i = 0; i < 5;i++) {
                if(i == index)
                    continue;
                viableColumns[i] = false;
            }
        }

        public int isSet(){
            int re = 0;
            int i = 0;
            int trues = 0;
            for(Boolean b:viableColumns){
                if(b){
                    re = i;
                    trues++;
                }
                i++;
            }
            if(trues < 1)
                return 10;
            return re;
        }

    }

    public static class Clue{

        public ClueType ClueType;

        public String[] StringClueValue;

        public int[] IntClueValue = new int[3];

        public Clue(ClueType t, String[] c){
            ClueType = t;
            StringClueValue = c;
            ToIntClueValue(c);
        }

        private void ToIntClueValue(String[] c) {
            int i = 0;
            for(String s:c){
                switch (s){
                    case "<":
                    case "\\^":
                        IntClueValue[i] = 0;
                        break;
                    case "A":
                        IntClueValue[i] = 1;
                        break;
                    case "B":
                        IntClueValue[i] = 2;
                        break;
                    case "C":
                        IntClueValue[i] = 3;
                        break;
                    case "D":
                        IntClueValue[i] = 4;
                        break;
                    case "E":
                        IntClueValue[i] = 5;
                        break;
                    case "F":
                        IntClueValue[i] = 6;
                        break;
                    case "G":
                        IntClueValue[i] = 7;
                        break;
                    case "H":
                        IntClueValue[i] = 8;
                        break;
                    case "I":
                        IntClueValue[i] = 9;
                        break;
                    case "J":
                        IntClueValue[i] = 10;
                        break;
                    case "K":
                        IntClueValue[i] = 11;
                        break;
                    case "L":
                        IntClueValue[i] = 12;
                        break;
                    case "M":
                        IntClueValue[i] = 13;
                        break;
                    case "N":
                        IntClueValue[i] = 14;
                        break;
                    case "O":
                        IntClueValue[i] = 15;
                        break;
                    case "P":
                        IntClueValue[i] = 16;
                        break;
                    case "Q":
                        IntClueValue[i] = 17;
                        break;
                    case "R":
                        IntClueValue[i] = 18;
                        break;
                    case "S":
                        IntClueValue[i] = 19;
                        break;
                    case "T":
                        IntClueValue[i] = 20;
                        break;
                }
                i++;
            }
        }
    }

}