Skip to content

Instantly share code, notes, and snippets.

@death
Created January 9, 2018 12:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save death/54d5019684e0fe71a9fd9c8933f1fc92 to your computer and use it in GitHub Desktop.
Save death/54d5019684e0fe71a9fd9c8933f1fc92 to your computer and use it in GitHub Desktop.
(defpackage #:snippets/cryptography-problem
(:use #:cl #:screamer #:screamer+)
(:shadowing-import-from #:screamer #:defun))
(in-package #:snippets/cryptography-problem)
;; S E N D
;; M O R E
;; =========
;; M O N E Y
(defmacro as-integer (&rest digits)
`(+v ,@(loop for d in digits
for i from (- (length digits) 1) downto 0
collect `(*v ,d ,(expt 10 i)))))
(defun cryptography-problem ()
(let ((vars (loop repeat 8 collect (an-integer-betweenv 0 9))))
(destructuring-bind (s e n d m o r y) vars
(assert! (all-differentv s e n d m o r y))
(assert! (=v (+v (as-integer s e n d)
(as-integer m o r e))
(as-integer m o n e y)))
(solution vars (static-ordering #'linear-force)))))
;; (one-value (cryptography-problem))
;; ==> (2 8 1 7 0 3 6 5)
;;
;; (all-values (cryptography-problem))
;; ==> ((2 8 1 7 0 3 6 5) (2 8 1 9 0 3 6 7) (3 7 1 2 0 4 6 9) (3 7 1 9 0 4 5 6)
;; (3 8 2 1 0 4 6 9) (3 8 2 9 0 4 5 7) (5 7 3 1 0 6 4 8) (5 7 3 2 0 6 4 9)
;; (5 8 4 9 0 6 3 7) (6 4 1 5 0 7 3 9) (6 4 1 9 0 7 2 3) (6 5 2 4 0 7 3 9)
;; (6 8 5 1 0 7 3 9) (6 8 5 3 0 7 2 1) (7 3 1 6 0 8 2 9) (7 4 2 9 0 8 1 3)
;; (7 5 3 1 0 8 2 6) (7 5 3 4 0 8 2 9) (7 5 3 9 0 8 1 4) (7 6 4 3 0 8 2 9)
;; (7 6 4 9 0 8 1 5) (8 3 2 4 0 9 1 7) (8 4 3 2 0 9 1 6) (8 5 4 2 0 9 1 7)
;; (9 5 6 7 1 0 8 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment