fork download
  1. (ns daily-programmer.20161121_easy.core
  2. (:use [clojure.string :only [split-lines]]))
  3.  
  4. (def wire-rules-m {"white" {:allowed #{} :disallowed #{"white" "black"}}
  5. "red" {:allowed #{"green"} :disallowed #{}}
  6. "black" {:allowed #{} :disallowed #{"white" "green" "orange"}}
  7. "orange" {:allowed #{"red" "black"} :disallowed #{}}
  8. "green" {:allowed #{"orange" "white"} :disallowed #{}}
  9. "purple" {:allowed #{} :disallowed #{"purple" "green" "orange" "white"}}})
  10.  
  11. (defn check [wire allowed disallowed]
  12. (and
  13. (or (empty? allowed) (contains? allowed wire))
  14. (or (empty? disallowed) (not (contains? disallowed wire)))))
  15.  
  16. (defn try-defuse [wires]
  17. (loop [[wire & other] wires
  18. allowed #{}
  19. disallowed #{}
  20. prev-cut true]
  21. (if (nil? wire)
  22. prev-cut
  23. (if-let [cut (check wire allowed disallowed)]
  24. (let [{:keys [allowed disallowed]} (get wire-rules-m wire)]
  25. (recur other allowed disallowed cut))
  26. false))))
  27.  
  28. (println (if (try-defuse (split-lines (slurp *in*))) "Bomb defused" "Boom"))
Success #stdin #stdout #stderr 0.68s 335488KB
stdin
white
orange
green
white
stdout
Boom
stderr
Java HotSpot(TM) Client VM warning: No monotonic clock was available - timed services may be adversely affected if the time-of-day clock changes