Created
May 12, 2015 08:59
-
-
Save snmsts/ef99ee312419f0c01d19 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 1 | |
(defun f (x) | |
(loop :with result := 0 | |
:for i :in x | |
:do (incf result i) | |
:finally (return result))) | |
(defun f (x) | |
(loop :with result := 0 | |
:for i :from 0 :to (1- (length x)) | |
:do (incf result (nth i x)) | |
:finally (return result))) | |
(defun f (x) | |
(if x | |
(+ (car x) | |
(f (cdr x))) | |
0)) | |
;; 2 | |
(defun mix (a b) | |
(loop :for a- :in a | |
:for b- :in b | |
:collect a- | |
:collect b-)) | |
(let ((hash (make-hash-table))) | |
(defun fib (x) | |
(if (gethash x hash) | |
(gethash x hash) | |
(setf (gethash x hash) (cond ((= x 0) 0) | |
((= x 1) 1) | |
(t (+ (fib (1- x)) (fib (- x 2))))))))) | |
(defun fib-list () | |
(loop :for i :from 0 :to 100 | |
:collect (fib i))) | |
;;3 | |
(defun biggest (x) | |
(format nil "~{~A~}" | |
(sort x #'(lambda (x y) | |
(loop | |
:with x := (format nil "~A" x) | |
:with y := (format nil "~A" y) | |
:with len := (1- (max (length x) (length y))) | |
:for i :from 0 :to len | |
:for x- := (digit-char-p (or (ignore-errors (aref x i)) #\0)) | |
:for y- := (digit-char-p (or (ignore-errors (aref y i)) #\0)) | |
:do (cond ((< x- y-) (return nil)) | |
((< y- x-) (return t)) | |
((= x y) | |
(when (ignore-errors (aref x i)) (return t)) | |
(when (ignore-errors (aref y i)) (return nil))))))))) | |
(biggest '(50 2 1 9)) | |
;;4 | |
(defun make-pattern (pattern) | |
(cdr (mix (cons '#:x (loop :for i :from 1 :to 8 | |
:for num := pattern :then (truncate num 3) | |
:for x := (rem num 3) | |
:collect (case x | |
(0 '+) | |
(1 '-) | |
(2 '_)))) | |
'(1 2 3 4 5 6 7 8 9)))) | |
(defun calc (x) | |
(let ((st)) | |
(loop | |
:for list :on x | |
:for head := (first list) | |
:do (cond ((numberp head) | |
(push head st)) | |
((or (eq head '-) (eq head '+)) | |
(push head st)) | |
((eq head '_) | |
(let ((x (+ (* (pop st) 10) (second list)))) | |
(pop list) | |
(push x st))))) | |
(setq st (nreverse st)) | |
(loop | |
:while (cdr st) | |
:do (push (funcall (second st) | |
(pop st) | |
(progn (pop st) | |
(pop st))) st)) | |
(first st))) | |
(defun puzzle () | |
(loop :for j :from 0 :to (1- (expt 3 8)) | |
:for pattern := (make-pattern j) | |
:when (= 100 (calc pattern)) | |
:do (print (list pattern (calc pattern))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment