Last active
January 30, 2020 19:46
-
-
Save stuartstein777/1d594ac36ba548792152f01d4cb1e8be to your computer and use it in GitHub Desktop.
secret-string 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
;; 1. Get each of the characters from the triplet as keys to a hash-map | |
;; 2. The values are the characters before the key character in the secret string. | |
;; 3. Find the key which has no values. | |
;; 4. Add that character to the final string | |
;; 5. Remove it from the hash-map, and all instances of it from the map values. | |
;; 6. Goto 3 | |
;; 7. Once hash-map has no keys left. We are done! | |
(ns codewars.puzzle-strings | |
(:require [clojure.set :as set] | |
[com.rpl.specter :as sc])) | |
(defn recover-secret [xs] | |
(loop [result "" | |
remaining (reduce #(as-> (update %1 (second %2) conj (first %2)) o | |
(update o (nth %2 2) conj (first %2) (second %2)) | |
(update o (first %2) (fnil conj []))) {} xs)] | |
(let [curr ((set/map-invert remaining) [])] | |
(cond (nil? curr) result | |
:else (recur (str result curr) (sc/setval (sc/walker (fn [x] (= curr x))) sc/NONE remaining)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment