init.scm 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. (define-module (sudoku init))
  2. (use-modules (srfi srfi-1)
  3. (sudoku solver)
  4. (sudoku board)
  5. (sudoku square)
  6. (sudoku utils))
  7. (define-public (initial-board)
  8. (init-board (initial-board-string)))
  9. (define (init-board s)
  10. (init-board-candidates
  11. (init-board-values (make-board)
  12. (string->num-list s))))
  13. (define (init-board-candidates board)
  14. (init-rows (init-cols (init-boxes board))))
  15. (define (init-rows board)
  16. (init-group update-row board))
  17. (define (init-cols board)
  18. (init-group update-col board))
  19. (define (init-boxes board)
  20. (init-group update-box board))
  21. (define (init-group update-proc board)
  22. (fold (lambda (i n-board)
  23. (update-proc n-board i))
  24. board
  25. (iota 9)))
  26. (define (init-board-values board vals)
  27. (array-map! board set-value board (list->array 2 (rank1->rank2 vals)))
  28. board)
  29. (define (initial-board-string)
  30. "002090060040001008070420003500000300001060500003000006100057040600900020020080100") ;; easy
  31. ;; "000070605000004080200006070073000200500000004004000960080400003050200000706080000") ;; hard
  32. ;; "800000000003600000070090200050007000000045700000100030001000068008500010090000400") ;; most difficult