Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (require 'seq)
- (setq sudokuRow 9)
- (setq sudokuBox 3)
- (setq sudokuValidCharacters '(1 2 3 4 5 6 7 8 9))
- (setq sudokuStr "000008000200059000100000250008001460300000100904083000000500084000004900023000000")
- (setq
- arr
- (vconcat (seq-map (lambda (c) (- c ?0)) (string-to-vector sudokuStr)))
- )
- (defun buildRow (p)
- "Build sudoku row from position(p)"
- (let (
- (r (mod p sudokuRow))
- (rp (/ p sudokuRow))
- (count 0)
- (row '())
- )
- (while (< count sudokuRow)
- (if
- (not(= count r))
- (setq row (cons (+ count (* rp sudokuRow)) row))
- )
- (setq count (+ count 1))
- )
- row
- )
- )
- (defun buildCol (p)
- "Build sudoku column from position(p)"
- (let (
- (r (mod p sudokuRow))
- (rp (/ p sudokuRow))
- (count 0)
- (col '())
- )
- (while (< count sudokuRow)
- (if
- (not(= count rp))
- (setq col (cons (+ r (* count sudokuRow)) col))
- )
- (setq count (+ count 1))
- )
- col
- )
- )
- (defun buildBoxLst (p)
- "Build sudoku box list from position(p)"
- (cond
- ((= p 0) '(1 2))
- ((= p 1) '(-1 1))
- ((= p 2) '(-2 -1))
- )
- )
- (defun buildBox (p)
- "Build sudoku box from position(p)"
- (let* (
- (r (mod (/ p sudokuRow) sudokuBox))
- (c (mod p sudokuBox))
- (rl (buildBoxLst r))
- (cl (buildBoxLst c))
- )
- (seq-reduce
- (lambda (ar dr)
- (seq-reduce
- (lambda (ac dc) (cons (+ p dc (* dr sudokuRow)) ac))
- cl
- ar)
- )
- rl
- '()
- )
- )
- )
- (defun buildStartingCharacters (e)
- "Build sudoku starting characters from edges(e)"
- (seq-reduce
- (
- lambda
- (a d)
- (
- seq-filter
- (lambda (elt)
- (not(= (aref arr d) elt))
- )
- a
- )
- )
- e
- sudokuValidCharacters
- )
- )
- (defun chatToInt (c p)
- "Convert ascii character to integer"
- (let* (
- (edges (append (buildRow p) (buildCol p) (buildBox p)))
- (startingValues (buildStartingCharacters edges))
- )
- (
- vector
- p
- c
- (if (zerop c) nil t)
- edges
- startingValues
- )
- )
- )
- (setq sudokuLst (seq-map-indexed 'chatToInt arr))
- (defun solveIt (l)
- "Solve sudoku puzzle"
- (if
- l
- (let* (
- (elem (car l))
- (p (aref elem 0))
- (v (aref elem 1))
- (f (aref elem 2))
- (ed (aref elem 3))
- (sv (aref elem 4))
- )
- (if f
- (solveIt (cdr l))
- (
- seq-do
- (lambda (s)
- (if (seq-find (lambda (e) (= (aref arr e) s)) ed)
- ()
- (progn
- (aset arr p s)
- (solveIt (cdr l))
- (aset arr p 0)
- )
- )
- )
- sv
- )
- )
- )
- (pp arr)
- )
- )
- (solveIt sudokuLst)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement