Created
June 4, 2014 03:35
-
-
Save sashton/aa4840065caa77829de4 to your computer and use it in GitHub Desktop.
This file contains 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 clojure-kata.core | |
(:require [clojure.test :refer :all])) | |
(defn nil-add | |
"Nil-safe add" | |
[& args] | |
(->> args | |
(filter identity) | |
(apply +))) | |
(defn bowl | |
[scores] | |
(reductions + | |
(let [flat (flatten scores)] | |
(loop [frames flat result []] | |
(if (and (seq frames) (< (count result) 10)) | |
(let [[f1 f2 f3 _] frames] | |
(cond | |
(= 10 f1) | |
(recur (drop 1 frames) (conj result (nil-add f1 f2 f3))) | |
(= 10 (+ f1 f2)) | |
(recur (drop 2 frames) (conj result (nil-add f1 f2 f3))) | |
:else | |
(recur (drop 2 frames) (conj result (nil-add f1 f2))))) | |
result))))) | |
(deftest one-frame | |
(is (= [5] (bowl [[2 3]])))) | |
(deftest two-frame | |
(is (= [5 12] (bowl [[2 3] [5 2]])))) | |
(deftest one-frame-strike | |
(is (= [10] (bowl [[10]])))) | |
(deftest two-frame-strike | |
(is (= [13 16] (bowl [[10] [1 2]])))) | |
(deftest double-strike | |
(is (= [20 30] (bowl [[10] [10]])))) | |
(deftest triple-strike | |
(is (= [30 50 60] (bowl [[10] [10] [10]])))) | |
(deftest one-frame-spare | |
(is (= [10] (bowl [[9 1]])))) | |
(deftest two-frame-spare | |
(is (= [16 23] (bowl [[9 1] [6 1]])))) | |
(deftest perfect-game | |
(is (= [30 60 90 120 150 180 210 240 270 300] | |
(bowl (conj (vec (repeat 9 [10])) [10 10 10]))))) | |
(deftest near-perfect-game | |
(is (= [30 60 90 120 150 180 210 240 270 295] | |
(bowl (conj (vec (repeat 9 [10])) [10 10 5]))))) | |
(deftest near-perfect-game | |
(is (= [30 60 90 120 150 180 210 236 256 267] | |
(bowl (conj (vec (repeat 9 [10])) [6 4 1]))))) | |
(deftest near-perfect-game2 | |
(is (= [30 60 90 120 150 180 210 230 250 260] | |
(bowl (conj (vec (repeat 9 [10])) [0 10 0]))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment