HomeAboutCodePastes
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordieggsy <dieggsy@pm.me>2021-04-24 20:26:35 -0400
committerdieggsy <dieggsy@pm.me>2021-04-24 20:26:35 -0400
commitf09fd9ce9eb06585c76e01910f4dd4071a939156 (patch)
tree23b3de6df3b2932903ee9e28343bb596bbb96d9c
parent7298d2412e4f70b6cf6d68f485ad00876e949288 (diff)
downloadproject-euler-f09fd9ce9eb06585c76e01910f4dd4071a939156.tar.gz
Add p59 solution
-rwxr-xr-xsbcl/p59.lisp50
1 files changed, 50 insertions, 0 deletions
diff --git a/sbcl/p59.lisp b/sbcl/p59.lisp
new file mode 100755
index 0000000..9dc650a
--- /dev/null
+++ b/sbcl/p59.lisp
@@ -0,0 +1,50 @@
+#!/usr/bin/sbcl --script
+
+(require :uiop)
+
+(defparameter +message+
+ (with-open-file (in (uiop:parse-unix-namestring
+ "../data/p59-msg.txt"))
+ (loop :for num = (read in nil)
+ :while num
+ :collect num))
+ "Message as an integer list")
+
+(defparameter +lower+
+ (mapcar #'char-code (coerce "abcdefghijklmnopqrstuvwxyz" 'list))
+ "ASCII codes for lowercase letters")
+
+(defparameter +all-codes+
+ (loop :for a :in +lower+
+ :append
+ (loop :for b :in +lower+
+ :append
+ (loop :for c :in +lower+
+ :collect (list a b c))))
+ "Every possible three byte code, lol - we could do the bulk of the work in the
+loop above if we wanted to save memory, probably.")
+
+(defun decrypt (code message)
+ "Decrypt an entire message using a three-byte CODE"
+ (loop :for i :on message :by #'cdddr
+ :append (mapcar #'logxor code
+ (subseq i 0 3))))
+
+(defun simple-decrypt (code three)
+ "Decrypt list of 3 bytes THREE using three-byte CODE"
+ (coerce (mapcar #'code-char (mapcar #'logxor code three)) 'string))
+
+
+(defun p59 ()
+ "For every code, iterate over message by three bytes, decrypting them and pick
+the first one that contains three common english words."
+ (loop :named outer :for code :in +all-codes+
+ :as wordcount = (loop :for three :on +message+ :by #'cdddr
+ :if (member (simple-decrypt code (subseq three 0 3))
+ '("and" "for" "the")
+ :test #'string=)
+ :sum 1)
+ :if (> wordcount 3)
+ :return (apply #'+ (decrypt code +message+))))
+
+(time (princ (p59)))