/
formats.lisp
61 lines (46 loc) · 1.78 KB
/
formats.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
(in-package #:sudoku)
;;; ***********************************
;;; Converting grid to universal format
;;; ***********************************
(defun collect-grids (grid-lines)
(labels ((collect-rec (lines &optional acc)
(if (null lines) (nreverse acc)
(collect-rec (subseq lines 10)
(push (subseq lines 1 10)
acc)))))
(collect-rec grid-lines)))
(defun multi-line->single-line (grid)
(apply #'concatenate 'string grid))
(defun dot->zero (grid)
(substitute #\0 #\. grid))
(defun grid->universal-format (grid)
(dot->zero (multi-line->single-line grid)))
;;; ***********************************
;;; Converting grid to universal format
;;; ***********************************
(defun line->universal-format (line)
(dot->zero line))
;;; ***************************************************
;;; Converting universal format to a board, an array
;;; ***************************************************
(defun universal-format->numeric-board (board)
(mapcar #'(lambda (c)
(parse-integer (string c)))
(coerce board 'list)))
(defun numeric-board->board (board)
(make-array 81 :initial-contents board))
(defun universal-format->board (board)
(numeric-board->board
(universal-format->numeric-board board)))
;;; *****************************************
;;; Converting a grid to a board, an array
;;; *****************************************
(defun grid->board (grid)
(universal-format->board
(grid->universal-format grid)))
;;; *****************************************
;;; Converting a grid to a board, an array
;;; *****************************************
(defun line->board (line)
(universal-format->board
(line->universal-format line)))