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.0.0.5 tracks/lisp/exercises/crypto-square/example.lisp
trackler-2.0.0.4 tracks/lisp/exercises/crypto-square/example.lisp
trackler-2.0.0.3 tracks/lisp/exercises/crypto-square/example.lisp
trackler-2.0.0.2 tracks/lisp/exercises/crypto-square/example.lisp
trackler-2.0.0.1 tracks/lisp/exercises/crypto-square/example.lisp
trackler-2.0.0.0 tracks/lisp/exercises/crypto-square/example.lisp
trackler-1.0.4.1 tracks/lisp/exercises/crypto-square/example.lisp
trackler-1.0.4.0 tracks/lisp/exercises/crypto-square/example.lisp
trackler-1.0.3.0 tracks/lisp/exercises/crypto-square/example.lisp
trackler-1.0.2.1 tracks/lisp/exercises/crypto-square/example.lisp
trackler-1.0.2.0 tracks/lisp/exercises/crypto-square/example.lisp
trackler-1.0.1.2 tracks/lisp/exercises/crypto-square/example.lisp
trackler-1.0.1.1 tracks/lisp/exercises/crypto-square/example.lisp
trackler-1.0.1.0 tracks/lisp/exercises/crypto-square/example.lisp
trackler-1.0.0.1 tracks/lisp/exercises/crypto-square/example.lisp
trackler-1.0.0 tracks/lisp/exercises/crypto-square/example.lisp