Skip to content

Instantly share code, notes, and snippets.

@stuartstein777
Last active January 30, 2020 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuartstein777/1d594ac36ba548792152f01d4cb1e8be to your computer and use it in GitHub Desktop.
Save stuartstein777/1d594ac36ba548792152f01d4cb1e8be to your computer and use it in GitHub Desktop.
secret-string solution
;; 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