-
Notifications
You must be signed in to change notification settings - Fork 2
/
sudoku.scala
78 lines (63 loc) · 1.86 KB
/
sudoku.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
sealed trait Cell extends Product with Serializable
case class Fixed(e:Int) extends Cell
case class Undetermined(list:List[Int]) extends Cell
val sudoku = """
26 7 1
68 7 9
19 45
82 1 4
46 29
5 3 28
93 74
4 5 36
7 3 18
"""
val s = sudoku
.split("\n")
.slice(1,10)
.map(row=>row
.split("")
.slice(1,10)
.map(cell=>cell match {
case " " => Undetermined((1 to 9).toList)
case _ => Fixed(cell.toInt)
}
).toVector
).toVector
def myNeighbourRow(s: Vector[Vector[Cell]], p:(Int,Int), e:Int) = {
(!s(p._1).contains(Fixed(e))) || s(p._1)(p._2) == Fixed(e)
}
def myNeighbourColumn(s: Vector[Vector[Cell]], p:(Int,Int), e:Int) = {
val t = s.transpose
(!t(p._2).contains(Fixed(e))) || s(p._1)(p._2) == Fixed(e)
}
def myNeighbourCube(s: Vector[Vector[Cell]], p:(Int,Int), e:Int) = {
val ci = p._1 / 3
val cj = p._2 / 3
val t = s.grouped(3).toList(ci).map{x=>(x.grouped(3).toList)(cj)}.flatten
(!t.contains(Fixed(e))) || s(p._1)(p._2) == Fixed(e)
}
def filter(sn: Vector[Vector[Cell]]):Vector[Vector[Cell]] = {
val sn1:Vector[Vector[Cell]] = sn
.zipWithIndex
.map{row:(Vector[Cell],Int)=>row._1
.zipWithIndex
.map{
case (Undetermined(values),index_x)=>Undetermined(values
.filter(x=>myNeighbourRow(sn,(row._2,index_x),x))
.filter(x=>myNeighbourColumn(sn,(row._2,index_x),x))
.filter(x=>myNeighbourCube(sn,(row._2,index_x),x)))
case (x,_)=>x//_._1 // (Fixed(value),index_x)=>value//Vector(Fixed(value))
}
.map{
case Undetermined(values) if (values.size==1)=>Fixed(values(0))
case x => x
}
}
if(sn1==sn) sn
else filter(sn1)
//sn1
}
var solved = filter(s)
var printable = solved.map{_.map(_.asInstanceOf[Fixed].e).mkString("")}.mkString("\n")
println(printable)