Skip to content

Instantly share code, notes, and snippets.

@cades
Created February 22, 2014 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cades/9153082 to your computer and use it in GitHub Desktop.
Save cades/9153082 to your computer and use it in GitHub Desktop.
Solution to lisp-koan' scoring-project, Greed dice game.
(defun score (dice)
(let ((dice (sort dice #'>)))
(flet ((is-1-or-5 (x)
(or (= x 1) (= x 5)))
(triple-p (dice)
(and (= (length dice) 3) (apply #'= dice)))
(triple-score (dice)
(if (= (first dice) 1)
1000
(* 100 (first dice)))))
(cond ((null dice) 0)
;; when length = 1
((equal dice '(5)) 50)
((equal dice '(1)) 100)
((= (length dice) 1) 0)
;; when length = 2
((= (length dice) 2)
(+ (score (list (first dice)))
(score (rest dice))))
;; when length = 3
((= (length dice) 3)
(if (triple-p dice)
(triple-score dice)
(+ (score (subseq dice 0 1)) (score (subseq dice 1)))))
;; when length >= 4
(t
(+ (score (subseq dice 0 1)) (score (subseq dice 1))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment