Sha256: 0f89b06026a86851b429b85a80e2ab6e08fd19c5bfa6936b4811cec2fb9d01bb

Contents?: true

Size: 1.27 KB

Versions: 396

Compression:

Stored size: 1.27 KB

Contents

(in-package #:cl-user)
(defpackage #:crypto-square
  (:use #:cl)
  (:export #:encipher))
(in-package #:crypto-square)

(defun normalize (text)
  (string-downcase (remove-if-not #'alpha-char-p text)))

(defun matrix-transpose (matrix)
  (let ((new-matrix (make-array (array-dimensions matrix))))
    (do ((x 0 (incf x)))
        ((= x (array-dimension matrix 0)) new-matrix)
      (do ((y 0 (incf y)))
          ((< x y))
        (setf (aref new-matrix x y) (aref matrix y x)
              (aref new-matrix y x) (aref matrix x y))))))

(defun text->matrix (text)
  (let* ((n (ceiling (sqrt (length text))))
         (array (make-array (list n n) :initial-element #\Space)))
    (do ((i 0 (incf i)))
        ((= i (length text)) array)
      (setf (row-major-aref array i) (char text i)))))

(defun matrix->text (matrix)
  (do ((rows (list))
       (x 0 (incf x)))
      ((= x (array-total-size matrix))
       (coerce (reverse rows) 'string))
    (let ((c (row-major-aref matrix x)))
      (when (and (not (zerop x))
                 (zerop (mod x (array-dimension matrix 0))))
        (push #\Space rows))
      (when (char/= c #\Space)
        (push (row-major-aref matrix x) rows)))))

(defun encipher (plaintext)
  (matrix->text
   (matrix-transpose
    (text->matrix
     (normalize plaintext)))))

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/common-lisp/exercises/crypto-square/example.lisp
trackler-2.2.1.118 tracks/common-lisp/exercises/crypto-square/example.lisp
trackler-2.2.1.117 tracks/common-lisp/exercises/crypto-square/example.lisp
trackler-2.2.1.116 tracks/common-lisp/exercises/crypto-square/example.lisp
trackler-2.2.1.115 tracks/common-lisp/exercises/crypto-square/example.lisp
trackler-2.2.1.114 tracks/common-lisp/exercises/crypto-square/example.lisp
trackler-2.2.1.113 tracks/common-lisp/exercises/crypto-square/example.lisp
trackler-2.2.1.111 tracks/common-lisp/exercises/crypto-square/example.lisp
trackler-2.2.1.110 tracks/common-lisp/exercises/crypto-square/example.lisp
trackler-2.2.1.109 tracks/common-lisp/exercises/crypto-square/example.lisp
trackler-2.2.1.108 tracks/common-lisp/exercises/crypto-square/example.lisp
trackler-2.2.1.107 tracks/common-lisp/exercises/crypto-square/example.lisp
trackler-2.2.1.106 tracks/common-lisp/exercises/crypto-square/example.lisp
trackler-2.2.1.105 tracks/common-lisp/exercises/crypto-square/example.lisp
trackler-2.2.1.104 tracks/common-lisp/exercises/crypto-square/example.lisp
trackler-2.2.1.103 tracks/common-lisp/exercises/crypto-square/example.lisp
trackler-2.2.1.102 tracks/common-lisp/exercises/crypto-square/example.lisp
trackler-2.2.1.101 tracks/common-lisp/exercises/crypto-square/example.lisp
trackler-2.2.1.100 tracks/common-lisp/exercises/crypto-square/example.lisp
trackler-2.2.1.99 tracks/common-lisp/exercises/crypto-square/example.lisp