(define (getRow list num) (cond ((eq? num 0) (car list)) (else (getRow (cdr list) (- num 1))))) (define (charAt row col board) (string-ref (getRow board row) col)) (define (mod a b) (modulo a b)) (define (read-board) ; This will _actually_ read a board at some point '("800000010" "031008264" "090060000" "006000037" "000936000" "140000800" "000040070" "659300140" "010000008")) (define (string-set string n char) (string-append (substring string 0 n) (symbol->string char) (substring string (+ n 1) (string-length string)))) (define (board-insert row col char board) (let ((new-board '())) (do ((my-board board (cdr my-board))) ((null? my-board) new-board) (cond ((eq? (length new-board) row) (set! new-board (append new-board (list (string-set (car my-board) col char))))) (else (set! new-board (append new-board (list (car my-board))))))))) (define (valid? num row col board) (let ((valid #t) (num (integer->char (+ (char->integer #\0) num))) (br (mod row 3)) (bc (mod col 3))) (do ((count 0 (+ count 1))) ((or (not valid) (= count 9)) valid) (if (and (eq? (charAt row count board) num) (not (eq? row count))) (set! valid #f)) (if (and (eq? (charAt (+ br (mod count 3)) (+ bc (truncate (/ count 3))) board) num) (not (eq? (+ br (mod count 3)) row))) (not (eq? (+ bc (truncate (/ count 3))) col)) (set! valid #f)) (if (and (eq? (charAt count col board) num) (not (eq? col count))) (set! valid #f))))) (define (solve-board board) (let ((valid #t)) (do ((rowCount 0 (+ rowCount 1))) ((= rowCount 9)) (do ((colCount 0 (+ colCount 1))) ((= colCount 9)) (if (eq? (string-ref (getRow board rowCount) colCount) #\0) (do ((testCount 0 (+ testCount 1))) ; Ok, its a zero -- we need to solve for this digit ((= testCount 9)) (if (valid? testCount rowCount colCount board) (solve-board (board-insert rowCount colCount (string-ref (getRow board rowCount) colCount ) board)) (set! valid #f)))))) board))