Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package day1
- object day3 extends App{
- /***
- import scala.collection.immutable.Set
- var jetSet = Set("Boing","Airbus","Boing")
- jetSet+="X-wing"
- for(i<-jetSet){
- println(i)
- }
- val l = List(1,1,1,1,1,2,2,2,2,3,4,54,4,4,4,4)
- val s=l.toSet[Int]
- for (i<-s){
- println(i + ":" +l.count(_==i))
- }
- */
- /****
- val l = List("exam","important","homework","exam")
- for(i<-l.toSet[String]){
- println(i + ": "+l.count(_==i))
- }
- */
- /**
- val romans = Map[Int,String](1->"I",2->"II",3->"III",4->"IV",5->"V")
- println(romans(2))
- for((arabic,roman)<-romans){
- println(arabic+ " " + roman)
- }
- val capitals=Map[String,String]("Norway"->"Oslo","Danmark"->"Kobenhagen","Sweden"->"Stockholm")
- for((country,capital)<-capitals) {
- println("The Capital of " + country + " is " + capital)
- }
- class Conf{
- val username = "Vetle"
- val password = "hidden"
- }
- val c=new Conf()
- println(c)
- object configuration{
- val username="Vetle"
- val password="Hidden"
- }
- println(configuration.username+": "+configuration.password)
- object Logger{
- def logger(x:String)={
- println(java.util.Calendar.getInstance().getTime()+": "+x)
- }
- }
- Logger.logger("today was a sad day.")
- */
- /**
- implicit def doubleToInt(x:Double):Int={
- x.toInt
- }
- //val i:Int=doubleToInt(3.5);
- val i:Int = 3.5
- println(i)
- implicit def LengthOfString(x:String):Int={
- x.length
- }
- val k:Int="hola"
- println(k)
- println("heihei"/2)
- */
- class Square(boxNumber:Int,xNumber:Int,yNumber:Int,values:List[Int]=List(1,2,3,4),solved:Boolean=false){
- var possibleNumbers=values;
- val x=xNumber
- val y=yNumber
- val box=boxNumber
- var s=solved
- override def toString():String = {
- return "x: "+this.x+", y: "+ this.y+", values: "+this.possibleNumbers.mkString(",")
- }
- def setValue(solution:Int)={
- this.s = true
- if(solution<5 && solution>0) {
- this.possibleNumbers=List(solution)
- }
- }
- def removeValue(v:Int):Unit={
- if(this.s==false) {
- this.possibleNumbers=this.possibleNumbers.filter(_!=v)
- if(this.possibleNumbers.length==1){
- this.setValue(this.possibleNumbers(0))
- }
- }
- }
- }
- //var s1 = new Square(1,1,1)
- //println(s1)
- //s1=s1.removeValue(4);
- //println(s1)
- var allSquares = List[Square]();
- def getBoxFromXY(x:Int,y:Int)={
- (x,y) match {
- case(1|2,1|2) => 1
- case(3|4,1|2) => 2
- case(1|2,3|4) => 3
- case(x,y) => 4
- }
- }
- for(x<-List(1,2,3,4)){
- for(y<-List(1,2,3,4)){
- val s=new Square(getBoxFromXY(x,y),x,y)
- allSquares=allSquares :+ s
- }
- }
- def getAllFromX(x:Int):List[Square] = {
- return allSquares.filter{(s:Square)=>s.x==x}
- }
- def getAllFromY(y:Int):List[Square]={
- allSquares.filter{s:Square=>s.y==y}
- }
- def getAllFromBox(b:Int):List[Square]={
- allSquares.filter{s=>s.box==b}
- }
- def checkLine(i:Int):Boolean={
- var a=List()
- getAllFromX(i).filter(x=>x.s).foreach(k=>a:::k.possibleNumbers)
- a.distinct.size==a.size
- }
- def getSquare(x:Int,y:Int):Square={
- return allSquares.filter(_.x==x).filter(_.y==y)(0)
- }
- def setValue(x:Int,y:Int,solution:Int)={
- var s = getSquare(x,y)
- allSquares=allSquares.filter(_!=s)
- s.setValue(solution)
- allSquares=allSquares :+ s
- }
- def removeValue(x:Int,y:Int,wrongSolution:Int): Unit ={
- var s = getSquare(x,y)
- allSquares=allSquares.filter(_!=s)
- s.removeValue(wrongSolution)
- allSquares=allSquares :+ s
- }
- setValue(1,1,3)
- setValue(1,2,4)
- setValue(1,3,1)
- setValue(2,2,2)
- setValue(3,3,2)
- setValue(4,2,1)
- setValue(4,3,4)
- setValue(4,4,3)
- def printIt(): Unit ={
- for(x<-List(1,2,3,4)){
- for(y<-List(1,2,3,4)){
- var s= getSquare(x,y)
- print(s.possibleNumbers)
- }
- println("")
- }
- }
- printIt()
- def printItNice(): Unit ={
- println(" ")
- for(x<-List(1,2,3,4)){
- for(y<-List(1,2,3,4)){
- var s= getSquare(x,y)
- if(s.possibleNumbers.length==1) {
- print(" "+s.possibleNumbers(0)+" ")
- } else {
- print(" ? ")
- }
- }
- println("")
- }
- println(" ")
- }
- printItNice()
- def isValid(x:Int,y:Int,solution:Int):Boolean={
- for(oneSquare<-(getAllFromX(x):::getAllFromY(y):::getAllFromBox(getBoxFromXY(x,y)) ) ){
- if(x!=oneSquare.x || y!=oneSquare.y){
- if(oneSquare.s==true && oneSquare.possibleNumbers(0)==solution){
- print("has solution:"+oneSquare.x,oneSquare.y,oneSquare.possibleNumbers)
- return false
- }
- }
- }
- return true
- }
- def removeIfNotValid(x:Int,y:Int,solution:Int): Unit ={
- if(!isValid(x,y,solution)){
- println("removing "+x,y,solution)
- removeValue(x,y,solution)
- }
- }
- for(x<-List(1,2,3,4)) {
- for (y <- List(1, 2, 3, 4)) {
- for (s <- List(1, 2, 3, 4)) {
- removeIfNotValid(x, y, s)
- }
- }
- }
- printIt()
- printItNice()
- /**
- def sum(a:Int, b:Int, c:Int)=a+b+c
- def sum2=sum(3,_:Int,6)
- println(sum2(2))
- def multiThree(x:Double,y:Double,z:Double): Double = x*y*z
- def multi2=multiThree(_:Double,_:Double,3.3)
- println(multi2(2.5,5.2))
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement