PasteRack.org
Paste # 40341
2021-04-15 05:42:12

Fork as a new paste.

Paste viewed 208 times.


Embed:

Without and with file I/O

  1. ;; Without file I/O
  2.  
  3. ; Board -> [Board | #f]
  4. ;
  5. ; Solve the Sudoku board `board` and return the solved board. If the board
  6. ; can't be solved, return `#f`.
  7. (define (solve-board board)
  8.   (unless (board-valid? board)
  9.     (raise-argument-error
  10.       'board
  11.       (format "valid board")
  12.       (board->string board)))
  13.   (define start-time (current-inexact-milliseconds))
  14.   (define guesses-count 0)
  15.   ; We can have at most as many recursion levels as there are cells on the
  16.   ; board.
  17.   (define recursion-stats (make-vector BOARD-SIZE 0))
  18.   (define result-board
  19.     (let solve-board-iter ([board board]
  20.                            [recursion-level 1])
  21.       (define next-index (next-index-for-empty-cell board))
  22.       (cond
  23.         [(eq? next-index #f)
  24.          ; Board complete
  25.          board]
  26.         [else
  27.          (vector-set!
  28.            recursion-stats
  29.            recursion-level
  30.            (add1 (vector-ref recursion-stats recursion-level)))
  31.          ; Try to solve the board with numbers 1-9 at the missing index.
  32.          (define new-candidate-board (board-copy board))
  33.          (for/or ([guess (in-range 1 (add1 9))])
  34.            (set! guesses-count (add1 guesses-count))
  35.            (board-set! new-candidate-board next-index guess)
  36.            (define board-valid? (index-valid? new-candidate-board next-index))
  37.            (if board-valid?
  38.                (solve-board-iter new-candidate-board (add1 recursion-level))
  39.                #f))])))
  40.   (Solver-Result
  41.     result-board
  42.     (- (current-inexact-milliseconds) start-time)
  43.     guesses-count
  44.     recursion-stats))
  45.  
  46. ;; Current WIP version with file I/O
  47.  
  48. ; Board -> [Board | #f]
  49. ;
  50. ; Solve the Sudoku board `board` and return the solved board. If the board
  51. ; can't be solved, return `#f`.
  52. (define (solve-board board)
  53.   (unless (board-valid? board)
  54.     (raise-argument-error
  55.       'board
  56.       (format "valid board")
  57.       (board->string board)))
  58.   (define start-time (current-inexact-milliseconds))
  59.   (define guesses-count 0)
  60.   ; We can have at most as many recursion levels as there are cells on the
  61.   ; board.
  62.   (define recursion-stats (make-vector BOARD-SIZE 0))
  63.   (define result-board
  64.     (with-output-to-file
  65.       "solver-data.txt" #:exists 'truncate #:mode 'text
  66.       (thunk
  67.         (displayln "# recursion level, index, guess")
  68.         (let solve-board-iter ([board board]
  69.                                [recursion-level 1])
  70.           (define next-index (next-index-for-empty-cell board))
  71.           (cond
  72.             [(eq? next-index #f)
  73.              ; Board complete
  74.              board]
  75.             [else
  76.              (vector-set!
  77.                recursion-stats
  78.                recursion-level
  79.                (add1 (vector-ref recursion-stats recursion-level)))
  80.              ; Try to solve the board with numbers 1-9 at the missing index.
  81.              (define new-candidate-board (board-copy board))
  82.              (for/or ([guess (in-range 1 (add1 9))])
  83.                (displayln (format "~a,~a,~a" recursion-level next-index guess))
  84.                (set! guesses-count (add1 guesses-count))
  85.                (board-set! new-candidate-board next-index guess)
  86.                (define board-valid? (index-valid? new-candidate-board next-index))
  87.                (if board-valid?
  88.                    (solve-board-iter new-candidate-board (add1 recursion-level))
  89.                    #f))])))))
  90.   (Solver-Result
  91.     result-board
  92.     (- (current-inexact-milliseconds) start-time)
  93.     guesses-count
  94.     recursion-stats))

=>

load-handler: expected a `module` declaration in "paste 40341", but found something else

Check that paste includes #lang?