Created
April 25, 2014 05:53
-
-
Save athos/11278926 to your computer and use it in GitHub Desktop.
a solution for http://www.objectmentor.com/resources/articles/xpepisode.htm
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
| (ns bowling-game) | |
| (defn- apply-to-nonnil [op & args] | |
| (->> (reverse args) | |
| (drop-while nil?) | |
| reverse | |
| (apply op))) | |
| (defn partition-frames [throws] | |
| (loop [n 1, [throw & more] throws, ret []] | |
| (cond (nil? throw) ret | |
| (= n 10) | |
| #_=> (let [[t1 t2 & more] more] | |
| (if (and t1 (>= (+ throw t1) 10)) | |
| (conj ret (apply-to-nonnil vector throw t1 t2)) | |
| (conj ret (apply-to-nonnil vector throw t1)))) | |
| (= throw 10) | |
| #_=> (recur (inc n) more (conj ret [throw])) | |
| :else | |
| #_=> (recur (inc n) (rest more) (conj ret [throw (first more)]))))) | |
| (defn- following-throws [frames] | |
| (apply concat frames)) | |
| (defn scores-for-frames [frames] | |
| (loop [frames frames, score 0, ret []] | |
| (if (empty? frames) | |
| ret | |
| (let [[x0 x1 x2] (following-throws frames) | |
| score (if (or (= x0 10) (and x1 (= (+ x0 x1) 10))) | |
| (apply-to-nonnil + score x0 x1 x2) | |
| (apply-to-nonnil + score x0 x1))] | |
| (recur (rest frames) score (conj ret score)))))) | |
| (defn game-score [frames] | |
| (last (scores-for-frames frames))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment