Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(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
You can’t perform that action at this time.