Skip to content

Instantly share code, notes, and snippets.

@gigasquid
Created May 9, 2012 13:04
Show Gist options
  • Save gigasquid/2644344 to your computer and use it in GitHub Desktop.
Save gigasquid/2644344 to your computer and use it in GitHub Desktop.
Berlin Clock Swarm
;; given 3 integers in a sequence
;; generate the number of lights that are on in a berlin clock,
;; returning 5 rows
(defn make-counts [[hours minutes seconds]]
[
(- 1 (mod seconds 2))
(int (/ hours 5))
(mod hours 5)
(int (/ minutes 5))
(mod minutes 5)
]
)
(defn make-line [active total]
(clojure.string/join
(concat (repeat active "*") (repeat (- total active) "."))))
;; given counts for 5 lines in a berlin clock
;; return sequence of 5 strings with the proper number of lights on
(defn make-lines [counts]
(map-indexed (fn [i x] (make-line (nth counts i) x))
'(1 4 4 11 4))
)
;; takes a sequence of 3
;; integers hours minutes seconds and returns string of berlin clock
(defn berlin-clock-print [time-parts]
(clojure.string/join "\n" (make-lines (make-counts time)))
)
;; return sequence of 3 integers
(defn parse-time [time-str]
(map #(Integer/parseInt %) (re-seq #"\d\d" time-str))
)
(println (parse-time "12:13:14"))
(println (berlin-clock-print (parse-time "21:22:05")))
(println (berlin-clock-print (parse-time "01:15:02")))
(println (berlin-clock-print (parse-time "01:00:01")))
(println (berlin-clock-print (parse-time "12:00:00")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment