This is my original solution:
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_clock.core) | |
(use '[clojure.string :only (join split)]) | |
(defn show [time] | |
(let | |
[[h m s] (map #(Integer. %) (split time #":")) | |
turn-on-red (fn [num-lamps] (repeat num-lamps "R")) | |
turn-on-yellow (fn [num-lamps] (repeat num-lamps "Y")) | |
turn-off (fn [num-lamps] (repeat num-lamps "O")) | |
turn-on-YYR (fn [num-lamps-on] | |
(take num-lamps-on (cycle ["Y" "Y" "R"]))) | |
show (comp (partial apply str) concat) | |
show-lamps (fn [num-lamps-on num-lamps turn-on] | |
(let [num-lamps-off (- num-lamps | |
num-lamps-on)] | |
(show (turn-on num-lamps-on) | |
(turn-off num-lamps-off)))) | |
seconds (if (zero? (rem s 2)) "Y" "O") | |
hours-first-row (show-lamps (quot h 5) 4 turn-on-red) | |
hours-second-row (show-lamps (rem h 5) 4 turn-on-red) | |
minutes-first-row (show-lamps (quot m 5) 11 turn-on-YYR) | |
minutes-second-row (show-lamps (rem m 5) 4 turn-on-yellow)] | |
(join "\n" | |
[seconds | |
hours-first-row | |
hours-second-row | |
minutes-first-row | |
minutes-second-row]))) |
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_clock.core) | |
(use '[clojure.string :only (join split)]) | |
(defn- turn-on-red [num-lamps] | |
(repeat num-lamps "R")) | |
(defn- turn-on-yellow [num-lamps] | |
(repeat num-lamps "Y")) | |
(defn- turn-off [num-lamps] | |
(repeat num-lamps "O")) | |
(defn- turn-on-YYR [num-lamps-on] | |
(take num-lamps-on (cycle ["Y" "Y" "R"]))) | |
(defn- show-lamps [num-lamps-on num-lamps turn-on] | |
(let [show (comp join concat) | |
num-lamps-off (- num-lamps num-lamps-on)] | |
(show (turn-on num-lamps-on) | |
(turn-off num-lamps-off)))) | |
(defn show [time] | |
(let | |
[[h m s] (map #(Integer. %) (split time #":")) | |
seconds-lamps-row (if (even? s) "Y" "O") | |
hours-lamps-first-row (show-lamps (quot h 5) 4 turn-on-red) | |
hours-lamps-second-row (show-lamps (rem h 5) 4 turn-on-red) | |
minutes-lamps-first-row (show-lamps (quot m 5) 11 turn-on-YYR) | |
minutes-lamps-second-row (show-lamps (rem m 5) 4 turn-on-yellow)] | |
(join "\n" | |
[seconds-lamps-row | |
hours-lamps-first-row | |
hours-lamps-second-row | |
minutes-lamps-first-row | |
minutes-lamps-second-row]))) |
No comments:
Post a Comment