Advertisement
Guest User

Untitled

a guest
Aug 31st, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. package day1
  2.  
  3. object day3 extends App{
  4. /***
  5. import scala.collection.immutable.Set
  6. var jetSet = Set("Boing","Airbus","Boing")
  7. jetSet+="X-wing"
  8. for(i<-jetSet){
  9. println(i)
  10. }
  11.  
  12. val l = List(1,1,1,1,1,2,2,2,2,3,4,54,4,4,4,4)
  13. val s=l.toSet[Int]
  14. for (i<-s){
  15. println(i + ":" +l.count(_==i))
  16. }
  17. */
  18. /****
  19. val l = List("exam","important","homework","exam")
  20. for(i<-l.toSet[String]){
  21. println(i + ": "+l.count(_==i))
  22. }
  23. */
  24. /**
  25. val romans = Map[Int,String](1->"I",2->"II",3->"III",4->"IV",5->"V")
  26. println(romans(2))
  27.  
  28. for((arabic,roman)<-romans){
  29. println(arabic+ " " + roman)
  30. }
  31.  
  32. val capitals=Map[String,String]("Norway"->"Oslo","Danmark"->"Kobenhagen","Sweden"->"Stockholm")
  33. for((country,capital)<-capitals) {
  34. println("The Capital of " + country + " is " + capital)
  35. }
  36.  
  37. class Conf{
  38. val username = "Vetle"
  39. val password = "hidden"
  40. }
  41. val c=new Conf()
  42. println(c)
  43.  
  44. object configuration{
  45. val username="Vetle"
  46. val password="Hidden"
  47. }
  48. println(configuration.username+": "+configuration.password)
  49.  
  50. object Logger{
  51. def logger(x:String)={
  52. println(java.util.Calendar.getInstance().getTime()+": "+x)
  53. }
  54. }
  55.  
  56. Logger.logger("today was a sad day.")
  57.  
  58. */
  59. /**
  60. implicit def doubleToInt(x:Double):Int={
  61. x.toInt
  62. }
  63.  
  64. //val i:Int=doubleToInt(3.5);
  65. val i:Int = 3.5
  66. println(i)
  67.  
  68. implicit def LengthOfString(x:String):Int={
  69. x.length
  70. }
  71.  
  72. val k:Int="hola"
  73. println(k)
  74. println("heihei"/2)
  75. */
  76.  
  77. class Square(boxNumber:Int,xNumber:Int,yNumber:Int,values:List[Int]=List(1,2,3,4),solved:Boolean=false){
  78. var possibleNumbers=values;
  79. val x=xNumber
  80. val y=yNumber
  81. val box=boxNumber
  82. var s=solved
  83.  
  84. override def toString():String = {
  85. return "x: "+this.x+", y: "+ this.y+", values: "+this.possibleNumbers.mkString(",")
  86. }
  87.  
  88. def setValue(solution:Int)={
  89. this.s = true
  90. if(solution<5 && solution>0) {
  91. this.possibleNumbers=List(solution)
  92. }
  93. }
  94.  
  95. def removeValue(v:Int):Unit={
  96. if(this.s==false) {
  97. this.possibleNumbers=this.possibleNumbers.filter(_!=v)
  98. if(this.possibleNumbers.length==1){
  99. this.setValue(this.possibleNumbers(0))
  100. }
  101. }
  102. }
  103. }
  104.  
  105. //var s1 = new Square(1,1,1)
  106. //println(s1)
  107. //s1=s1.removeValue(4);
  108. //println(s1)
  109.  
  110. var allSquares = List[Square]();
  111.  
  112. def getBoxFromXY(x:Int,y:Int)={
  113. (x,y) match {
  114. case(1|2,1|2) => 1
  115. case(3|4,1|2) => 2
  116. case(1|2,3|4) => 3
  117. case(x,y) => 4
  118. }
  119. }
  120.  
  121. for(x<-List(1,2,3,4)){
  122. for(y<-List(1,2,3,4)){
  123. val s=new Square(getBoxFromXY(x,y),x,y)
  124. allSquares=allSquares :+ s
  125. }
  126. }
  127.  
  128. def getAllFromX(x:Int):List[Square] = {
  129. return allSquares.filter{(s:Square)=>s.x==x}
  130. }
  131.  
  132. def getAllFromY(y:Int):List[Square]={
  133. allSquares.filter{s:Square=>s.y==y}
  134. }
  135. def getAllFromBox(b:Int):List[Square]={
  136. allSquares.filter{s=>s.box==b}
  137. }
  138.  
  139. def checkLine(i:Int):Boolean={
  140. var a=List()
  141. getAllFromX(i).filter(x=>x.s).foreach(k=>a:::k.possibleNumbers)
  142. a.distinct.size==a.size
  143. }
  144.  
  145. def getSquare(x:Int,y:Int):Square={
  146. return allSquares.filter(_.x==x).filter(_.y==y)(0)
  147. }
  148.  
  149. def setValue(x:Int,y:Int,solution:Int)={
  150. var s = getSquare(x,y)
  151. allSquares=allSquares.filter(_!=s)
  152. s.setValue(solution)
  153. allSquares=allSquares :+ s
  154. }
  155.  
  156. def removeValue(x:Int,y:Int,wrongSolution:Int): Unit ={
  157. var s = getSquare(x,y)
  158. allSquares=allSquares.filter(_!=s)
  159. s.removeValue(wrongSolution)
  160. allSquares=allSquares :+ s
  161. }
  162.  
  163. setValue(1,1,3)
  164. setValue(1,2,4)
  165. setValue(1,3,1)
  166. setValue(2,2,2)
  167. setValue(3,3,2)
  168. setValue(4,2,1)
  169. setValue(4,3,4)
  170. setValue(4,4,3)
  171.  
  172. def printIt(): Unit ={
  173. for(x<-List(1,2,3,4)){
  174. for(y<-List(1,2,3,4)){
  175. var s= getSquare(x,y)
  176. print(s.possibleNumbers)
  177. }
  178. println("")
  179. }
  180. }
  181.  
  182. printIt()
  183.  
  184. def printItNice(): Unit ={
  185. println(" ")
  186. for(x<-List(1,2,3,4)){
  187. for(y<-List(1,2,3,4)){
  188. var s= getSquare(x,y)
  189. if(s.possibleNumbers.length==1) {
  190. print(" "+s.possibleNumbers(0)+" ")
  191. } else {
  192. print(" ? ")
  193. }
  194. }
  195. println("")
  196. }
  197. println(" ")
  198. }
  199.  
  200. printItNice()
  201.  
  202. def isValid(x:Int,y:Int,solution:Int):Boolean={
  203. for(oneSquare<-(getAllFromX(x):::getAllFromY(y):::getAllFromBox(getBoxFromXY(x,y)) ) ){
  204. if(x!=oneSquare.x || y!=oneSquare.y){
  205. if(oneSquare.s==true && oneSquare.possibleNumbers(0)==solution){
  206. print("has solution:"+oneSquare.x,oneSquare.y,oneSquare.possibleNumbers)
  207. return false
  208. }
  209. }
  210. }
  211. return true
  212. }
  213.  
  214. def removeIfNotValid(x:Int,y:Int,solution:Int): Unit ={
  215. if(!isValid(x,y,solution)){
  216. println("removing "+x,y,solution)
  217. removeValue(x,y,solution)
  218. }
  219. }
  220.  
  221. for(x<-List(1,2,3,4)) {
  222. for (y <- List(1, 2, 3, 4)) {
  223. for (s <- List(1, 2, 3, 4)) {
  224. removeIfNotValid(x, y, s)
  225. }
  226. }
  227. }
  228.  
  229. printIt()
  230. printItNice()
  231. /**
  232. def sum(a:Int, b:Int, c:Int)=a+b+c
  233.  
  234. def sum2=sum(3,_:Int,6)
  235.  
  236. println(sum2(2))
  237.  
  238. def multiThree(x:Double,y:Double,z:Double): Double = x*y*z
  239. def multi2=multiThree(_:Double,_:Double,3.3)
  240.  
  241. println(multi2(2.5,5.2))
  242. */
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement