096_1.scm 971 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. ;; Sudoku
  2. (use-modules (sudoku solver)
  3. (sudoku init)
  4. (sudoku square)
  5. (rnrs io ports)
  6. (euler utils))
  7. (define (sudoku-sum)
  8. (define board-generator (make-board-generator))
  9. (let loop ((acc 0))
  10. (let ((board (board-generator)))
  11. (if (not board) acc
  12. (loop (+ acc
  13. (get-top-left-number
  14. (solver board))))))))
  15. ;; Not sure if the unless thing is a good idea...
  16. (define (make-board-generator)
  17. (let ((file (open-input-file "../input/p096.txt")))
  18. (lambda ()
  19. (cond
  20. [(port-closed? file) #f]
  21. [(eof-object? (get-line file)) (close-input-port file) #f]
  22. [else
  23. (let ((board-string ""))
  24. (do ((i 0 (1+ i)))
  25. ((>= i 9))
  26. (set! board-string (string-append board-string
  27. (get-line file))))
  28. (init-board board-string))]))))
  29. (define (get-top-left-number solved-board)
  30. (digits->number
  31. (map get-value
  32. (map (lambda (col)
  33. (array-ref solved-board 0 col))
  34. (iota 3)))))