progs/sudoku.scala
changeset 383 c02929f2647c
parent 329 8a34b2ebc8cc
child 384 6e1237691307
equal deleted inserted replaced
382:1bd800376e0c 383:c02929f2647c
    19 val indexes = (0 to 8).toList
    19 val indexes = (0 to 8).toList
    20 
    20 
    21 
    21 
    22 def empty(game: String) = game.indexOf(emptyValue)
    22 def empty(game: String) = game.indexOf(emptyValue)
    23 def isDone(game: String) = empty(game) == -1 
    23 def isDone(game: String) = empty(game) == -1 
    24 def emptyPosition(game: String) : Pos = 
    24 //def emptyPosition(game: String) : Pos = 
    25   (empty(game) % maxValue, empty(game) / maxValue)
    25 //  (empty(game) % maxValue, empty(game) / maxValue)
       
    26 def emptyPosition(game: String) = {
       
    27   val e = empty(game)
       
    28   (e % maxValue, e / maxValue)
       
    29 }
    26 
    30 
    27 
    31 
    28 def get_row(game: String, y: Int) = indexes.map(col => game(y * maxValue + col))
    32 def get_row(game: String, y: Int) = indexes.map(col => game(y * maxValue + col))
    29 def get_col(game: String, x: Int) = indexes.map(row => game(x + row * maxValue))
    33 def get_col(game: String, x: Int) = indexes.map(row => game(x + row * maxValue))
    30 
    34 
    63     }
    67     }
    64   }
    68   }
    65 }
    69 }
    66 
    70 
    67 def pretty(game: String): String = 
    71 def pretty(game: String): String = 
    68   "\n" + (game.sliding(maxValue, maxValue).mkString(",\n"))
    72   "\n" + (game.grouped(maxValue).mkString(",\n"))
    69 
    73 
    70 
    74 
    71 // a list of hard games according to Andrew Coles and Peter Norvig
    75 // a list of hard games according to Andrew Coles and Peter Norvig
    72 
    76 
    73 val hard_games = 
    77 val hard_games = 
    76        "48.3............71.2.......7.5....6....2..8.............1.76...3.....4......5....",
    80        "48.3............71.2.......7.5....6....2..8.............1.76...3.....4......5....",
    77        "......52..8.4......3...9...5.1...6..2..7........3.....6...1..........7.4.......3.",
    81        "......52..8.4......3...9...5.1...6..2..7........3.....6...1..........7.4.......3.",
    78        "6.2.5.........3.4..........43...8....1....2........7..5..27...........81...6.....",
    82        "6.2.5.........3.4..........43...8....1....2........7..5..27...........81...6.....",
    79        ".524.........7.1..............8.2...3.....6...9.5.....1.6.3...........897........",
    83        ".524.........7.1..............8.2...3.....6...9.5.....1.6.3...........897........",
    80        "6.2.5.........4.3..........43...8....1....2........7..5..27...........81...6.....",
    84        "6.2.5.........4.3..........43...8....1....2........7..5..27...........81...6.....",
    81        ".923.........8.1...........1.7.4...........658.........6.5.2...4.....7.....9.....",
    85        ".923.........8.1...........1.7.4...........658.........6.5.2...4.....7.....9.....")
    82 
    86 /*
    83        "85...24..72......9..4.........1.7..23.5...9...4...........8..7..17..........36.4.",
    87        "85...24..72......9..4.........1.7..23.5...9...4...........8..7..17..........36.4.",
    84        "..53.....8......2..7..1.5..4....53...1..7...6..32...8..6.5....9..4....3......97..",
    88        "..53.....8......2..7..1.5..4....53...1..7...6..32...8..6.5....9..4....3......97..",
    85        "12..4......5.69.1...9...5.........7.7...52.9..3......2.9.6...5.4..9..8.1..3...9.4",
    89        "12..4......5.69.1...9...5.........7.7...52.9..3......2.9.6...5.4..9..8.1..3...9.4",
    86        "...57..3.1......2.7...234......8...4..7..4...49....6.5.42...3.....7..9....18.....",
    90        "...57..3.1......2.7...234......8...4..7..4...49....6.5.42...3.....7..9....18.....",
    87        "7..1523........92....3.....1....47.8.......6............9...5.6.4.9.7...8....6.1.",
    91        "7..1523........92....3.....1....47.8.......6............9...5.6.4.9.7...8....6.1.",
   181        ".5..9....1.....6.....3.8.....8.4...9514.......3....2..........4.8...6..77..15..6.",
   185        ".5..9....1.....6.....3.8.....8.4...9514.......3....2..........4.8...6..77..15..6.",
   182        ".....2.......7...17..3...9.8..7......2.89.6...13..6....9..5.824.....891..........",
   186        ".....2.......7...17..3...9.8..7......2.89.6...13..6....9..5.824.....891..........",
   183        "3...8.......7....51..............36...2..4....7...........6.13..452...........8..",
   187        "3...8.......7....51..............36...2..4....7...........6.13..452...........8..",
   184        "....14....3....2...7..........9...3.6.1.............8.2.....1.4....5.6.....7.8...",
   188        "....14....3....2...7..........9...3.6.1.............8.2.....1.4....5.6.....7.8...",
   185        "4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......")
   189        "4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......")
   186 
   190 */
   187 println("108:\n" ++ pretty(hard_games(108).replaceAll("\\.", " ")) ++ "\n")
   191 //println("108:\n" ++ pretty(hard_games(108).replaceAll("\\.", " ")) ++ "\n")
   188 println("109:\n" ++ pretty(hard_games(109).replaceAll("\\.", " ")) ++ "\n")
   192 //println("109:\n" ++ pretty(hard_games(109).replaceAll("\\.", " ")) ++ "\n")
   189 
   193 
   190 
   194 
   191 // for measuring time
   195 // for measuring time
   192 def time_needed[T](i: Int, code: => T) = {
   196 def time_needed[T](i: Int, code: => T) = {
   193   val start = System.nanoTime()
   197   val start = System.nanoTime()