Skip to content

Instantly share code, notes, and snippets.

@alex-hofsteede
Created June 3, 2014 17:50
Show Gist options
  • Save alex-hofsteede/eefcd6a890cd1e33ed01 to your computer and use it in GitHub Desktop.
Save alex-hofsteede/eefcd6a890cd1e33ed01 to your computer and use it in GitHub Desktop.
(define (unit-valid unit)
(equal? (sort unit <) (list '1 '2 '3 '4 '5 '6 '7 '8 '9)))
(define (units-valid func lst n)
(if (eqv? n 8)
(unit-valid (func lst n 0))
(and (units-valid func lst (+ n 1)) (unit-valid (func lst n 0)))))
(define (nth-row lst n i)
(let ((cell (list (list-ref lst (+ (* n 9) i)))))
(if (eqv? i '8) cell (append cell (nth-row lst n (+ i 1))))))
(define (nth-col lst n i)
(let ((cell (list (list-ref lst (+ (* i 9) n)))))
(if (eqv? i '8) cell (append cell (nth-col lst n (+ i 1))))))
(define (nth-box lst n i)
(let ((cell (list (list-ref lst (+ (* (quotient n 3) 27) (* (modulo n 3) 3) (* (quotient i 3) 9) (modulo i 3))))))
(if (eqv? i '8) cell (append cell (nth-box lst n (+ i 1))))))
(define (valid str)
(let ((grid (map char->digit (string->list str))))
(and (eqv? (length grid) '81)
(units-valid nth-row grid 0)
(units-valid nth-box grid 0)
(units-valid nth-col grid 0))))
(and (valid "751843926893625174642179583425316798176982345938754612364297851289531467517468239")
(not (valid "751843927893625174642179583425316798176982345938754612364297851289531467517468239"))
(not (valid "571843926893625174642179583425316798176982345938754612364297851289531467517468239"))
(not (valid "851743926693825174142679583425316798976182345738954612364297851289531467517468239")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment