Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Last active September 7, 2024 10:17
Show Gist options
  • Save xuwei-k/8f29247e7242f84f1160de7189505cd5 to your computer and use it in GitHub Desktop.
Save xuwei-k/8f29247e7242f84f1160de7189505cd5 to your computer and use it in GitHub Desktop.
scalaVersion := "3.5.1-RC2"
import scala.compiletime.ops.boolean.&&
type Sudoku[A <: Tuple] = A match {
case (
_1_1 *: _1_2 *: _1_3 *: _1_4 *: _1_5 *: _1_6 *: _1_7 *: _1_8 *: _1_9 *:
_2_1 *: _2_2 *: _2_3 *: _2_4 *: _2_5 *: _2_6 *: _2_7 *: _2_8 *: _2_9 *:
_3_1 *: _3_2 *: _3_3 *: _3_4 *: _3_5 *: _3_6 *: _3_7 *: _3_8 *: _3_9 *:
_4_1 *: _4_2 *: _4_3 *: _4_4 *: _4_5 *: _4_6 *: _4_7 *: _4_8 *: _4_9 *:
_5_1 *: _5_2 *: _5_3 *: _5_4 *: _5_5 *: _5_6 *: _5_7 *: _5_8 *: _5_9 *:
_6_1 *: _6_2 *: _6_3 *: _6_4 *: _6_5 *: _6_6 *: _6_7 *: _6_8 *: _6_9 *:
_7_1 *: _7_2 *: _7_3 *: _7_4 *: _7_5 *: _7_6 *: _7_7 *: _7_8 *: _7_9 *:
_8_1 *: _8_2 *: _8_3 *: _8_4 *: _8_5 *: _8_6 *: _8_7 *: _8_8 *: _8_9 *:
_9_1 *: _9_2 *: _9_3 *: _9_4 *: _9_5 *: _9_6 *: _9_7 *: _9_8 *: _9_9 *: EmptyTuple
) =>
(
CheckNumber[A] &&
Check[_1_1 *: _1_2 *: _1_3 *: _1_4 *: _1_5 *: _1_6 *: _1_7 *: _1_8 *: _1_9 *: EmptyTuple] &&
Check[_2_1 *: _2_2 *: _2_3 *: _2_4 *: _2_5 *: _2_6 *: _2_7 *: _2_8 *: _2_9 *: EmptyTuple] &&
Check[_3_1 *: _3_2 *: _3_3 *: _3_4 *: _3_5 *: _3_6 *: _3_7 *: _3_8 *: _3_9 *: EmptyTuple] &&
Check[_4_1 *: _4_2 *: _4_3 *: _4_4 *: _4_5 *: _4_6 *: _4_7 *: _4_8 *: _4_9 *: EmptyTuple] &&
Check[_5_1 *: _5_2 *: _5_3 *: _5_4 *: _5_5 *: _5_6 *: _5_7 *: _5_8 *: _5_9 *: EmptyTuple] &&
Check[_6_1 *: _6_2 *: _6_3 *: _6_4 *: _6_5 *: _6_6 *: _6_7 *: _6_8 *: _6_9 *: EmptyTuple] &&
Check[_7_1 *: _7_2 *: _7_3 *: _7_4 *: _7_5 *: _7_6 *: _7_7 *: _7_8 *: _7_9 *: EmptyTuple] &&
Check[_8_1 *: _8_2 *: _8_3 *: _8_4 *: _8_5 *: _8_6 *: _8_7 *: _8_8 *: _8_9 *: EmptyTuple] &&
Check[_9_1 *: _9_2 *: _9_3 *: _9_4 *: _9_5 *: _9_6 *: _9_7 *: _9_8 *: _9_9 *: EmptyTuple] &&
Check[_1_1 *: _2_1 *: _3_1 *: _4_1 *: _5_1 *: _6_1 *: _7_1 *: _8_1 *: _9_1 *: EmptyTuple] &&
Check[_1_2 *: _2_2 *: _3_2 *: _4_2 *: _5_2 *: _6_2 *: _7_2 *: _8_2 *: _9_2 *: EmptyTuple] &&
Check[_1_3 *: _2_3 *: _3_3 *: _4_3 *: _5_3 *: _6_3 *: _7_3 *: _8_3 *: _9_3 *: EmptyTuple] &&
Check[_1_4 *: _2_4 *: _3_4 *: _4_4 *: _5_4 *: _6_4 *: _7_4 *: _8_4 *: _9_4 *: EmptyTuple] &&
Check[_1_5 *: _2_5 *: _3_5 *: _4_5 *: _5_5 *: _6_5 *: _7_5 *: _8_5 *: _9_5 *: EmptyTuple] &&
Check[_1_6 *: _2_6 *: _3_6 *: _4_6 *: _5_6 *: _6_6 *: _7_6 *: _8_6 *: _9_6 *: EmptyTuple] &&
Check[_1_7 *: _2_7 *: _3_7 *: _4_7 *: _5_7 *: _6_7 *: _7_7 *: _8_7 *: _9_7 *: EmptyTuple] &&
Check[_1_8 *: _2_8 *: _3_8 *: _4_8 *: _5_8 *: _6_8 *: _7_8 *: _8_8 *: _9_8 *: EmptyTuple] &&
Check[_1_9 *: _2_9 *: _3_9 *: _4_9 *: _5_9 *: _6_9 *: _7_9 *: _8_9 *: _9_9 *: EmptyTuple] &&
Check[_1_1 *: _1_2 *: _1_3 *: _2_1 *: _2_2 *: _2_3 *: _3_1 *: _3_2 *: _3_3 *: EmptyTuple] &&
Check[_1_4 *: _1_5 *: _1_6 *: _2_4 *: _2_5 *: _2_6 *: _3_4 *: _3_5 *: _3_6 *: EmptyTuple] &&
Check[_1_7 *: _1_8 *: _1_9 *: _2_7 *: _2_8 *: _2_9 *: _3_7 *: _3_8 *: _3_9 *: EmptyTuple] &&
Check[_4_1 *: _4_2 *: _4_3 *: _5_1 *: _5_2 *: _5_3 *: _6_1 *: _6_2 *: _6_3 *: EmptyTuple] &&
Check[_4_4 *: _4_5 *: _4_6 *: _5_4 *: _5_5 *: _5_6 *: _6_4 *: _6_5 *: _6_6 *: EmptyTuple] &&
Check[_4_7 *: _4_8 *: _4_9 *: _5_7 *: _5_8 *: _5_9 *: _6_7 *: _6_8 *: _6_9 *: EmptyTuple] &&
Check[_7_1 *: _7_2 *: _7_3 *: _8_1 *: _8_2 *: _8_3 *: _9_1 *: _9_2 *: _9_3 *: EmptyTuple] &&
Check[_7_4 *: _7_5 *: _7_6 *: _8_4 *: _8_5 *: _8_6 *: _9_4 *: _9_5 *: _9_6 *: EmptyTuple] &&
Check[_7_7 *: _7_8 *: _7_9 *: _8_7 *: _8_8 *: _8_9 *: _9_7 *: _9_8 *: _9_9 *: EmptyTuple]
) match {
case true =>
true
}
}
type Check[A <: Tuple] <: Boolean =
Tuple.Size[Distinct[A]] match {
case 9 =>
true
case _ =>
false
}
type CheckNumber[A <: Tuple] <: Boolean = A match {
case 1 *: xs =>
CheckNumber[xs]
case 2 *: xs =>
CheckNumber[xs]
case 3 *: xs =>
CheckNumber[xs]
case 4 *: xs =>
CheckNumber[xs]
case 5 *: xs =>
CheckNumber[xs]
case 6 *: xs =>
CheckNumber[xs]
case 7 *: xs =>
CheckNumber[xs]
case 8 *: xs =>
CheckNumber[xs]
case 9 *: xs =>
CheckNumber[xs]
case EmptyTuple =>
true
}
type Distinct[XS <: Tuple] <: Tuple = XS match {
case _ =>
Distinct0[XS, EmptyTuple]
}
type Distinct0[XS <: Tuple, ACC <: Tuple] <: Tuple = XS match {
case a1 *: a2 =>
Tuple.Contains[ACC, a1] match {
case true =>
Distinct0[a2, ACC]
case false =>
Distinct0[a2, Tuple.Append[ACC, a1]]
}
case EmptyTuple =>
ACC
}
object Main {
def main(args: Array[String]): Unit = {
println(valueOf[Sudoku[(
9,5,3, 8,1,7, 4,2,6,
6,8,1, 4,9,2, 3,5,7,
2,7,4, 5,6,3, 8,1,9,
8,9,7, 1,3,4, 5,6,2,
5,1,2, 6,7,8, 9,3,4,
4,3,6, 9,2,5, 7,8,1,
7,4,8, 2,5,6, 1,9,3,
1,2,5, 3,4,9, 6,7,8,
3,6,9, 7,8,1, 2,4,5,
)]])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment