Created
January 7, 2013 19:08
-
-
Save sortega/4477553 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 berlin.core) | |
(defn composed-lights [amount] | |
[(quot amount 5) | |
(rem amount 5)]) | |
(defn seconds-lights [ss] | |
(if (even? ss) 1 0)) | |
(defn lights [[hh mm ss]] | |
(flatten [(seconds-lights ss) | |
(composed-lights hh) | |
(composed-lights mm)])) | |
(defn parse-time [t] | |
(when-let [[_ & fields] (re-matches #"(\d\d):(\d\d):(\d\d)" t)] | |
(map read-string fields))) | |
(def fully-lighten [[:y] | |
[:r :r :r :r] | |
[:r :r :r :r] | |
[:y :y :r :y :y :r :y :y :r :y :y] | |
[:y :y :y :y]]) | |
(defn partial-row [row n] | |
(let [len (count row)] | |
(-> [] | |
(into (take n row)) | |
(into (repeat (- len n) :o))))) | |
(defn format-lights [lights] | |
(map partial-row fully-lighten lights)) | |
(defn berlin-uhr | |
"Converts a time in hh:mm:ss format to the Berlin Uhr one. | |
Output will be a vector of vectors of lights encoded as | |
:o off, :y yellow, :r red" | |
[t] | |
(format-lights (lights (parse-time t)))) |
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 berlin.core-test | |
(:use clojure.test | |
berlin.core)) | |
(deftest berlin-uhr-test | |
(testing "Trivial time" | |
(is (= [[:y] | |
[:o :o :o :o] | |
[:o :o :o :o] | |
[:o :o :o :o :o :o :o :o :o :o :o] | |
[:o :o :o :o]] | |
(berlin-uhr "00:00:00")))) | |
(testing "Odd seconds" | |
(is (= [[:o] | |
[:o :o :o :o] | |
[:o :o :o :o] | |
[:o :o :o :o :o :o :o :o :o :o :o] | |
[:o :o :o :o]] | |
(berlin-uhr "00:00:01")))) | |
(testing "With hours" | |
(is (= [[:y] | |
[:r :r :o :o] | |
[:r :r :r :o] | |
[:o :o :o :o :o :o :o :o :o :o :o] | |
[:o :o :o :o]] | |
(berlin-uhr "13:00:00")))) | |
(testing "Almost midnight" | |
(is (= [[:o] | |
[:r :r :r :r] | |
[:r :r :r :o] | |
[:y :y :r :y :y :r :y :y :r :y :y] | |
[:y :y :y :y]] | |
(berlin-uhr "23:59:59")))) | |
(testing "Midnight" | |
(is (= [[:y] | |
[:r :r :r :r] | |
[:r :r :r :r] | |
[:o :o :o :o :o :o :o :o :o :o :o] | |
[:o :o :o :o]] | |
(berlin-uhr "24:00:00"))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment