Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io._;
- import scala.io._;
- object Skyscraper extends App {
- def updateTrueValues(
- north:Array[String],
- west:Array[String],
- east:Array[String],
- south:Array[String],
- board:Array[Array[SkyscraperPuzzleField]],
- puzzleSize:Int
- ):Array[Array[SkyscraperPuzzleField]] =
- {
- var b = board.clone;
- for(n<-0 to puzzleSize-1)
- {
- if(north(n).toInt==1)
- {
- b(n)(0).setTrueList(puzzleSize);
- }
- if(east(n).toInt==1)
- {
- b(puzzleSize-1)(n).setTrueList(puzzleSize);
- }
- if(south(n).toInt==1)
- {
- b(n)(puzzleSize-1).setTrueList(puzzleSize);
- }
- if(west(n).toInt==1)
- {
- b(0)(n).setTrueList(puzzleSize);
- }
- if(north(n).toInt==puzzleSize)
- {
- for(i<-1 to puzzleSize)
- {
- b(n)(i-1).setTrueList(i);
- }
- }
- if(east(n).toInt==puzzleSize)
- {
- for(i<-puzzleSize to 1 by -1)
- {
- b(i-1)(n).setTrueList(i);
- }
- }
- if(south(n).toInt==puzzleSize)
- {
- for(i<-puzzleSize to 1 by -1)
- {
- b(n)(i-1).setTrueList(i);
- }
- }
- if(west(n).toInt==puzzleSize)
- {
- for(i<-1 to puzzleSize)
- {
- b(i-1)(n).setTrueList(i);
- }
- }
- //this is where I would process the hint numbers between 1 and puzzleSize once the algorithm was developed
- /*for(i<-2 to (puzzleSize-1))
- {
- if(north(n)==i)
- {
- b(n)(0).removeFromTrueList(...);
- }
- if(east(n)==i)
- {
- ...
- }
- if(south(n)==i)
- {
- ...
- }
- if(west(n)==i)
- {
- ...
- }
- }*/
- //etc unfinished algorithm
- }
- return b;
- }
- val inputdir = """C:\skyscraper\Input""";
- val outputdir = """C:\skyscraper\Output""";
- val dir = new File(inputdir);
- for(f<-dir.listFiles()){
- solveSkySkraper(f)
- }
- def solveSkySkraper(f:File):Unit = {
- f.getName()
- val lines = Source.fromFile(f).mkString
- val numPuzzles = lines(0)
- val out = new PrintWriter( new File(outputdir+"\\"+f.getName()) , "UTF-8")
- out.println(numPuzzles)
- var lineOffset:Int = 0;
- for(i<-0 until numPuzzles.asDigit)
- {
- var puzzleSize = 0;
- var tracker = 0;
- var northHints:Array[String] = Array();
- var westHints:Array[String] = Array();
- var eastHints:Array[String] = Array();
- var southHints:Array[String] = Array();
- for (line <- Source.fromFile(f).getLines)
- {
- if(tracker==1+lineOffset)
- {
- puzzleSize = augmentString(line).toInt;
- }
- else if(tracker==2+lineOffset)
- {
- northHints = line split " ";
- }
- else if(tracker==3+lineOffset)
- {
- westHints = line split " ";
- }
- else if(tracker==4+lineOffset)
- {
- eastHints = line split " ";
- }
- else if(tracker==5+lineOffset)
- {
- southHints = line split " ";
- }
- tracker=tracker+1;
- }
- val puzzleBoard = Array.fill[SkyscraperPuzzleField](puzzleSize, puzzleSize){new SkyscraperPuzzleField(puzzleSize)};
- val puzzleBoardRefined = updateTrueValues(northHints, westHints,eastHints,southHints,puzzleBoard,puzzleSize);
- var solutionString:String="";
- out.println(puzzleSize);
- for(i<-0 to (puzzleSize-1))
- {
- for(n<-0 to (puzzleSize-1))
- {
- solutionString=solutionString+puzzleBoardRefined(n)(i).getTrueList.mkString+" ";
- }
- out.println(solutionString)
- solutionString="";
- }
- /*println(northHints.mkString);
- println(westHints.mkString);
- println(eastHints.mkString);
- println(southHints.mkString);*/
- lineOffset=lineOffset+5;
- }
- out.close();
- }
- }
- class SkyscraperPuzzleField(size:Int) {
- var trueList = for(i<-1 to size)yield i;
- def removeFromTrueList(x:Int) =
- {
- trueList = for(i<-1 to size if (i!=x))yield i;
- }
- def setTrueList(x:Int) =
- {
- trueList = for(i<-1 to size if(i==x))yield i;
- }
- def getTrueListNumber():Int =
- {
- trueList(0);
- }
- def getTrueList =
- {
- trueList;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement