Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Created September 7, 2024 11:32
Show Gist options
  • Save xuwei-k/ce902faba0351a3a2ebd8c6421da8f69 to your computer and use it in GitHub Desktop.
Save xuwei-k/ce902faba0351a3a2ebd8c6421da8f69 to your computer and use it in GitHub Desktop.
sudoku
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 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 Placeholder *: xs =>
CheckNumber[xs]
case EmptyTuple =>
true
}
type Placeholder = " "
type - = Placeholder
type Check[XS <: Tuple] = XS match {
case Placeholder *: a2 =>
Check[a2]
case a1 *: a2 =>
Tuple.Contains[a2, a1] match {
case true =>
false
case false =>
Check[a2]
}
case EmptyTuple =>
true
}
object Main {
def main(args: Array[String]): Unit = {
valueOf[Sudoku[(
- , 3 , - , - , - , - , - , - , -,
- , - , - , 1 , 9 , 5 , - , - , -,
- , - , 8 , - , - , - , - , 6 , -,
8 , - , - , - , 6 , - , - , - , -,
4 , - , - , 8 , - , - , - , - , 1,
- , - , - , - , 2 , - , - , - , -,
- , 6 , - , - , - , - , 2 , 8 , 9,
- , - , - , 4 , 1 , 9 , 6 , 3 , 5,
- , - , - , - , - , - , - , 7 , -,
)]]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment