diff --git a/sudoku.lispbm b/sudoku.lispbm index ffe5140..77406d4 100644 --- a/sudoku.lispbm +++ b/sudoku.lispbm @@ -27,6 +27,17 @@ (defun amount (x y) (foldr (lambda (z q) (if (eq x z) (+ q 1) q)) 0 y)) +(defun in (x xs) (foldr (lambda (y z) (if (eq x y) t z)) nil xs)) + +(defun intersection (xs ys) (filter (lambda (x) (in x ys)) xs)) + +(defun repeat (x e) + (if (number? x) + (match x + (0 nil) + (_ (cons e (repeat (- x 1) e)))) + 'error)) + (defun valid (x) (if (= 9 (length x)) (foldr @@ -35,8 +46,36 @@ (map (lambda (y) (amount y x)) nums)) nil)) +(defun notPresentRow (xs) + (foldr + (lambda (x ys) (if (in x xs) ys (cons x ys))) + nil + nums)) + +(defun notPresent (grid row col) + (intersection (notPresentRow (getRow grid row)) + (notPresentRow (getCol grid col)))) + (defun getRow (grid x) (ix grid x)) +(defun findEmpty (grid) + (foldr (lambda (zs ys) + (match zs + (((?row) . (?rowVal)) + (append + (zip + (repeat 9 row) + (foldr (lambda (x qs) + (match x + (((?col) . (?val)) (if (eq val nil) (cons col qs) qs)) + (_ 'error))) + nil + (zip (iota 9) rowVal))) + ys)) + (_ 'error))) + nil + (zip (iota 9) grid))) + (defun transpose (grid) (match grid (nil nil) @@ -59,7 +98,6 @@ (let ((f (lambda (x start stop) (take (drop x start) (- (+ 1 stop) start))))) (map (lambda (x) (f x startcol stopcol)) (f m startrow stoprow)))) - (defun getSubGridAsList (m x) (if (and (>= x 0) (< x 9)) (let ((sr (* (// x 3) 3)) @@ -68,4 +106,3 @@ (ec (+ sc 2))) (matrixToList (getSubMatrix m sr er sc ec))) 'error)) -