Created
November 7, 2016 16:37
-
-
Save visibletrap/6b1af8076ab1a02837b8d5799a8d55a8 to your computer and use it in GitHub Desktop.
Clojure solution for https://www.hackerrank.com/challenges/sequence-full-of-colors
This file contains hidden or 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 hackerrank.sequence-full-of-colors.core) | |
| (defn count-color [codes color] | |
| (->> codes | |
| (filter #(= color %)) | |
| (count))) | |
| (defn eq [codes & colors] | |
| (->> colors | |
| (map #(count-color codes %)) | |
| (apply =))) | |
| (defn prefix-diff [check-fn codes colors] | |
| (->> colors | |
| (map #(count-color codes %)) | |
| (apply -) | |
| (Math/abs) | |
| (check-fn))) | |
| (defn diff<=? [limit codes & colors] | |
| (->> codes | |
| (reduce (fn [[result current] code] | |
| (if (prefix-diff #(<= % limit) current colors) | |
| [result (conj current code)] | |
| (reduced [false]))) | |
| [true []]) | |
| (first))) | |
| (defn full-of-colors? [codes] | |
| (and (eq codes \R \G) | |
| (eq codes \Y \B) | |
| (diff<=? 1 codes \R \G) | |
| (diff<=? 1 codes \Y \B))) | |
| (comment | |
| (map full-of-colors? ["RGGR" "RYBG" "RYRB" "YGYGRBRB"])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment