Skip to content

Instantly share code, notes, and snippets.

@GregMefford
Last active August 29, 2015 14:10
Show Gist options
  • Save GregMefford/2caa51a9801ceaa7d82e to your computer and use it in GitHub Desktop.
Save GregMefford/2caa51a9801ceaa7d82e to your computer and use it in GitHub Desktop.
Cincy Day of Functional
(require '[clojure.string :as str])
(ns berlin
(:require [clojure.test :refer :all]))
(defn render-lights [number-on total-lights]
(let [on-lights (apply str (repeat number-on "*"))
format-string (str "%-" total-lights "s")]
(str/replace
(format format-string on-lights)
#" "
".")))
(defn second-light [s]
(render-lights (mod s 2) 1))
(defn hour-lights [h]
(list
(render-lights (int (/ h 5)) 4)
(render-lights (mod h 5) 4)))
(defn minute-lights [m]
(list
(render-lights (int (/ m 5)) 11)
(render-lights (mod m 5) 4)))
(defn berlin-clock [[h m s]]
(flatten (list
(second-light s)
(hour-lights h)
(minute-lights m))))
(defn parse-time [time-string]
(map
(fn [s] (Integer. s))
(str/split time-string #":")))
(deftest can-make-times
(is (= '("*" "...." "...." "..........." "....") (berlin-clock (parse-time "00:00:00"))))
(is (= '("." "**.." "***." "***........" "**..") (berlin-clock (parse-time "13:17:01"))))
(is (= '("." "****" "***." "***********" "****") (berlin-clock (parse-time "23:59:59"))))
)
(run-tests)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment