Created
February 6, 2013 10:07
-
-
Save sortega/4721591 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)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment