Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io._
- import scala.collection.mutable.MutableList
- import sun.security.util.Length
- import com.sun.org.apache.xpath.internal.operations.Bool
- import com.sun.corba.se.spi.orbutil.fsm.Guard
- object Solver extends App {
- val inputdir = "E:\\dev\\input"; // Change to real input dir
- val outputdir = "E:\\dev\\input"; // Change to real output dir
- val WHITE = 1;
- val BLUE = 2;
- var long:Long=0;
- import java.io.File;
- val dir = new File(inputdir);
- for(f<-dir.listFiles()){
- solveSkySkraper(f)
- }
- // A class representing a single square of the puzzle.
- class Square (xNumber:Int, yNumber:Int, values:List[Int]=List(1,2), solved:Boolean=false) {
- val x = xNumber;
- val y = yNumber;
- val s = solved;
- val possibleValues = values;
- /**
- * Sends in a solution, and removes all other solution to that square.
- */
- def setValue(solution:Int, square:Square):Square = {
- val solved = new Square(square.x,square.y,List(solution), true);
- return solved;
- }
- /**
- * Removes a given value from the list of possible values.
- */
- def removeValue(wrongSolution:Int, square:Square):Square = {
- if(wrongSolution == 1) {
- return setValue(2, square);
- }
- else {
- return setValue(1, square);
- }
- }
- override def toString ():String={
- return possibleValues.toString;
- }
- }
- def solveSkySkraper(f:File):Unit = {
- println(f.getName())
- val lines = scala.io.Source.fromFile(f).mkString.split("\n")
- val numPuzzles:Int = lines(0).trim().toInt;
- // Keeps this two non functional.
- var numRows:Int = lines(1).trim().toInt;
- var currLine:Int = 2;
- var list = MutableList[String]();
- for( a <- 1 to numPuzzles ){
- for ( a <- 1 to numRows){
- list +=lines(currLine);
- currLine+=1;
- }
- if(a != numPuzzles) {
- println("Finished parsing game, going on to start solving...");
- // SOLVE
- translateRaw(list);
- // Continue if there are more games in the file...
- numRows = lines(currLine).trim().toInt+1;
- currLine+=1;
- list.clear();
- }
- else {
- // println("This was the last game in file, solving it...");
- //list.foreach(println)
- var listSq = translateRaw(list);
- val t0 = System.nanoTime()
- SolvePuzzles(listSq);
- val t1 = System.nanoTime()
- println();
- println("Elapsed time: " + (t1 - t0)/1000000 + "ms")
- }
- }
- /*
- * Translates the raw list, to a valid format of Square objects.
- */
- def translateRaw (unparsed:MutableList[String]):MutableList[Square] = {
- var list = MutableList[Square]();
- var row = 0;
- for(x<-unparsed) {
- for(a<-0 to x.size-1) {
- val char = x.charAt(a).toString;
- if(char == ".") {
- var sq = (new Square(a,row,List(1,2),false));
- list+= sq;
- }
- if(char == "w") {
- var sq = (new Square(a,row,List(1),true));
- list+= sq;
- }
- if(char == "b") {
- var sq = (new Square(a,row,List(2),true));
- list+= sq;
- }
- }
- row+=1;
- }
- return list;
- }
- def SolvePuzzles(list:MutableList[Square]) {
- var newList = list;
- var listCopy = newList;
- newList = checkForForcedSolution(newList);
- newList = checkForLineAndColumns(newList);
- var hello = bruteForcer(newList);
- listCopy = hello._1;
- var b = hello._2;
- println (b);
- printIt(listCopy);
- println();
- println("time spent replacing squares: " + long/1000000 + "ms")
- }
- def bruteForcer (list:MutableList[Square]):(MutableList[Square],Boolean) = {
- var prevSave = list;
- prevSave = checkForForcedSolution(prevSave);
- prevSave = checkForLineAndColumns(prevSave);
- var next = getNextUnsolved(list);
- var osquare = next._1 ;
- var square = osquare;
- var continue = next._2;
- if(!continue) {
- println("stopping");
- return (prevSave, true)
- }
- //checks 1
- square = square.setValue(1, square);
- var t0 = System.nanoTime()
- prevSave = replaceSquare(square, prevSave);
- var t1 = System.nanoTime()
- long= long+(t1-t0);
- if(checkIfValid(prevSave)) {
- var res = bruteForcer(prevSave) //
- if (res._2){
- prevSave=res._1;
- return (prevSave,true);}
- }
- //checks 2
- square = square.setValue(2, square);
- var t3 = System.nanoTime()
- prevSave = replaceSquare(square, prevSave);
- var t4 = System.nanoTime()
- long= long+(t4-t3);
- if(checkIfValid(prevSave)) {
- var res = bruteForcer(prevSave) //
- if (res._2){
- prevSave=res._1;
- return (prevSave,true);}
- }
- // prevSave = replaceSquare(osquare,prevSave);
- return (prevSave,false);
- }
- /**
- * Gives you the next square that is unsolved, returns false if
- * everything is solved.
- */
- def getNextUnsolved (list:MutableList[Square]):(Square, Boolean) = {
- for( a <- 0 to math.sqrt(list.length).toInt-1){
- for( b <- 0 to math.sqrt(list.length).toInt-1){
- if(!getSquare(b, a, list).s) {
- return (getSquare(b, a, list),true);
- }
- }
- }
- return (getSquare(0, 0, list),false)
- }
- def checkIfValid(list:MutableList[Square]):Boolean = {
- for( b <- 0 to math.sqrt(list.length).toInt-1){
- var column = getAllFromX(b, list);
- var line = getAllFromY(b, list);
- if(!(checkIfLineIsValid(column))) {
- return false;
- }
- if(!(checkIfLineIsValid(line))) {
- return false;
- }
- }
- return true;
- }
- def checkIfLineIsValid (list:MutableList[Square]):Boolean = {
- var b = 0;
- var w = 0;
- var w_1 = 0;
- var b_1 = 0;
- for (a<- list) {
- if(a.s) {
- // Check for three in a row..
- if(a.possibleValues(0) == WHITE ) {
- w = w+1
- b = 0;
- w_1 = w_1+1;
- }
- else {
- b = b+1;
- w = 0;
- b_1 = b_1+1;
- }
- }
- else {
- b=0;
- w=0;
- }
- if(b == 3 || w == 3 || w_1 > list.length/2 || b_1 > list.length/2) {
- return false;
- }
- // Check for equal amount in lines..
- }
- return true;
- }
- def compareResults(list:MutableList[Square],list2:MutableList[Square]):Boolean = {
- var numberOfSolved1 = 0;
- var numberOfSolved2 = 0;
- for (a<-list) {
- if(a.s) {
- numberOfSolved1 = numberOfSolved1 +1;
- }
- }
- for(a<- list2) {
- if(a.s) {
- numberOfSolved2 = numberOfSolved2 +1;
- }
- }
- if(numberOfSolved1 == numberOfSolved2) {
- return true
- }
- return false;
- }
- def checkForForcedSolution(list:MutableList[Square]):MutableList[Square] = {
- var newList = list;
- for (a<-newList) {
- newList = removeIfNotValid(a, newList);
- }
- return newList;
- }
- def checkForLineAndColumns(list:MutableList[Square]):MutableList[Square] = {
- var newList = list;
- for( b <- 0 to math.sqrt(list.length).toInt-1){
- var column = getAllFromX(b, newList);
- var line = getAllFromY(b, newList);
- newList = lineCheck(line,newList);
- newList = lineCheck(column,newList)
- }
- return newList;
- }
- def removeIfNotValid(square:Square, list:MutableList[Square]):MutableList[Square]={
- var newList = list;
- var tuple = isForced(square, list);
- if(tuple._1){
- if(tuple._2 == 1) {
- var square_t = square.removeValue(1, square)
- square_t.s == true;
- newList = replaceSquare(square_t, list);
- }
- else {
- var square_t = square.removeValue(2, square)
- square_t.s == true;
- newList = replaceSquare(square_t, list);
- }
- }
- return newList;
- }
- /**
- * Check a sqaure's x & y axis for even amounts of b&w.
- */
- def lineCheck(line:MutableList[Square], list:MutableList[Square]):MutableList[Square] = {
- var solution = list;
- var white = 0;
- var blue = 0;
- // Check all line..
- for(a<-line) {
- if(a.s) {
- if(a.possibleValues(0) == WHITE) {
- white = white+1;
- }
- else {
- blue = blue + 1;
- }
- }
- }
- if((white == line.length/2) && !(blue == line.length/2)) {
- for(a <- line) {
- if(!a.s){
- var temp_s = a.setValue(BLUE , a);
- solution = replaceSquare(temp_s, solution);
- }
- }
- }
- if((blue == line.length/2) && !(white == line.length/2)) {
- for(a <- line) {
- if(!a.s){
- var temp_s = a.setValue(WHITE , a);
- solution = replaceSquare(temp_s, solution);
- }
- }
- }
- return solution;
- }
- /**
- * Checks if a square is forced to a value, by the predefined known values.
- * This function checks two squares, in each direction.
- */
- def isForced (square:Square, list:MutableList[Square]):(Boolean, Int) = {
- val x = square.x;
- val y = square.y;
- // Checking left
- if(checkForced(getSquare(x-1, y, list), getSquare(x-2, y-0, list))) {
- return (true,getSquare(x-1, y, list).possibleValues(0));
- }
- // Checking down
- else if(checkForced(getSquare(x, y+1, list), getSquare(x, y+2, list))) {
- return (true,getSquare(x, y+1, list).possibleValues(0));
- }
- // Checking right
- else if(checkForced(getSquare(x+1, y, list), getSquare(x+2, y, list))) {
- return (true,getSquare(x+1, y, list).possibleValues(0));
- }
- // Checking up
- else if(checkForced(getSquare(x, y-1, list), getSquare(x, y-2, list))) {
- return (true,getSquare(x, y-1, list).possibleValues(0));
- }
- // Checking inner horizontal
- else if(checkForced(getSquare(x-1, y, list), getSquare(x+1, y, list))) {
- return (true,getSquare(x-1, y, list).possibleValues(0));
- }
- // Checking inner vertical
- else if(checkForced(getSquare(x, y-1, list), getSquare(x, y+1, list))) {
- return (true,getSquare(x, y-1, list).possibleValues(0));
- }
- return (false,1);
- }
- /**
- * A subfunction for isForced(); checking if
- * two squares is equal to one another.
- */
- def checkForced(sq1:Square, sq2:Square):Boolean = {
- if(sq1.possibleValues(0) == sq2.possibleValues(0) && (sq1.s && sq2.s)) {
- return true;
- }
- else {
- return false;
- }
- }
- /**
- * Replace a given square in the solution list.
- */
- def replaceSquare(square:Square, list:MutableList[Square]):MutableList[Square] = {
- var newList = MutableList[Square]();
- for(a <- list) {
- if(a.x == square.x && a.y == square.y) {
- newList += (new Square(square.x,square.y,square.possibleValues,square.s));
- }
- else {
- newList += a;
- }
- }
- return newList;
- }
- def printIt(list:MutableList[Square]):Unit = {
- var a = 0;
- var b = 0;
- for( a <- 0 to math.sqrt(list.length).toInt-1){
- println();
- for( b <- 0 to math.sqrt(list.length).toInt-1){
- if(getSquare(b, a, list).s) {
- print("("+getSquare(b, a, list).possibleValues(0)+")");
- }
- else {
- print("(n)");
- }
- }
- }
- }
- def getSquare(x:Int,y:Int, allSquares:MutableList[Square]):Square = {
- if(x<0 || y < 0 || x > math.sqrt(allSquares.length)-1 || y > math.sqrt(allSquares.length)-1) {
- return new Square(0,0,List(1,2),false)
- }
- else {
- return allSquares.filter(_.x==x).filter(_.y==y)(0)
- }
- }
- def getAllFromX(i:Int, list:MutableList[Square]):MutableList[Square] = {
- return list.filter(_.x == i)
- }
- def getAllFromY(i:Int, list:MutableList[Square]):MutableList[Square] = {
- return list.filter(_.y == i)
- }
- /*
- var out = new PrintWriter( new File(outputdir+"\\"+f.getName()) , "UTF-8")
- out.print(numPuzzles + "\n")
- var sol = ""
- out.print("6\n") //Fake solution
- out.print("wwbwbb\n")
- out.print("bbwbww\n")
- out.print("wbwbwb\n")
- out.print("wwbwbb\n")
- out.print("bbwbww\n")
- out.print("bwbwbw\n")
- out.print("8\n")
- out.print("bwwbwwbb\n")
- out.print("bwbwbwbw\n")
- out.print("wbwwbbwb\n")
- out.print("wwbbwbbw\n")
- out.print("bbwwbwwb\n")
- out.print("wwbbwbbw\n")
- out.print("bbwbwbww\n")
- out.print("wbbwbwwb\n")
- out.close();
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement