Skip to content

Instantly share code, notes, and snippets.

@msszczep
Created April 8, 2018 21:17
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 msszczep/8495cdf828a0dbb5ca1ea576eb587147 to your computer and use it in GitHub Desktop.
Save msszczep/8495cdf828a0dbb5ca1ea576eb587147 to your computer and use it in GitHub Desktop.
In progress solutions for 4Clojure problem #112
(defn p112-take1 [n d]
(letfn [(recast-to-vec [acc]
(let [num-left (count (re-seq #"\[" (apply str acc)))
num-right (count (re-seq #"\]" (apply str acc)))
updated-seq (concat acc (repeat (- num-left num-right) "]"))]
(read-string (apply str (interpose " " updated-seq)))))
(recast-to-int [s]
(Integer. (clojure.string/replace s #"[\]\[]" "")))]
(loop [acc-value 0
acc-structure []
d-plus (clojure.string/split (str d) #" ")]
(if (< n (+ acc-value (recast-to-int (first d-plus))))
(recast-to-vec acc-structure)
(recur (+ acc-value (recast-to-int (first d-plus)))
(conj acc-structure (first d-plus))
(next d-plus))))))
(defn p112-take2 [n d]
(letfn [(get-value [e] (if (vector? (first e)) (ffirst e) e))]
(loop [acc-value 0
acc-structure []
d-plus d]
(if
(< n (+ acc-value (get-value (first d-plus))))
acc-structure
(recur (+ acc-value (get-value (first d-plus)))
(conj acc-structure (get-value (first d-plus)))
(next d-plus))))))
(defn p112-take3 [n d]
(letfn [(recast-to-vec [acc]
(let [num-left (count (re-seq #"\[" (apply str acc)))
num-right (count (re-seq #"\]" (apply str acc)))
updated-seq (concat acc (repeat (- num-left num-right) "]"))]
(read-string (apply str (interpose " " updated-seq)))))
(recast-to-int [s]
(Integer. (clojure.string/replace s #"[\]\[]" "")))]
(loop [acc-value 0
acc-structure []
d-plus (clojure.string/split (str d) #" ")]
(if (< n (+ acc-value (recast-to-int (first d-plus))))
(recast-to-vec acc-structure)
(recur (+ acc-value (recast-to-int (first d-plus)))
(conj acc-structure (first d-plus))
(next d-plus))))))
(defn p112-take4 [n d]
(letfn [(create-d-struct [s]
(let [num-left (count (filter #(= "[" %) s))
num-right (count (filter #(= "]" %) s))
updated-seq (concat s (repeat (- num-left num-right) "]"))]
(read-string (apply str (interpose " " updated-seq)))))
(get-seq-total [s]
(reduce + (flatten s)))
(format-d-string [s]
(remove clojure.string/blank? (->
(str "[" (apply str (interpose " " (take 25 s))) "]")
(clojure.string/replace #"[\-\d]+" #(str " " %1 " "))
(clojure.string/replace #"\[" #(str " " %1 " "))
(clojure.string/replace #"\]" #(str " " %1 " "))
(clojure.string/split #" "))) )]
(let [d-string (remove clojure.string/blank? (format-d-string d))
d-structs (loop [acc 1
d-struct []]
(if (= acc (count d-string))
d-struct
(recur (inc acc)
(if (= "[" (last (take acc d-string)))
d-struct
(conj d-struct
(create-d-struct (take acc d-string)))))))]
(->> d-structs
distinct
(take-while #(>= n (get-seq-total %)))
last
(into [])))))
(defn tests [f]
[(= (f 10 [1 2 [3 [4 5] 6] 7])
'(1 2 (3 (4))))
(= (f 30 [1 2 [3 [4 [5 [6 [7 8]] 9]] 10] 11])
'(1 2 (3 (4 (5 (6 (7)))))))
(= (f 1 [[[[[1]]]]])
'(((((1))))))
(= (f 0 [0 0 [0 [0]]])
'(0 0 (0 (0))))
(= (f 1 [-10 [1 [2 3 [4 5 [6 7 [8]]]]]])
'(-10 (1 (2 3 (4)))))
(= (f 0 [1 2 [3 [4 5] 6] 7])
'())
(= (f 9 (range))
'(0 1 2 3))
])
(defn t-with [f]
[(f 10 [1 2 [3 [4 5] 6] 7])
(f 30 [1 2 [3 [4 [5 [6 [7 8]] 9]] 10] 11])
(f 1 [[[[[1]]]]])
(f 0 [0 0 [0 [0]]])
(f 1 [-10 [1 [2 3 [4 5 [6 7 [8]]]]]])
(f 0 [1 2 [3 [4 5] 6] 7])
(f 9 (range))])
(defn t-without [f]
[(f 10 [1 2 [3 [4 5] 6] 7])
(f 30 [1 2 [3 [4 [5 [6 [7 8]] 9]] 10] 11])
(f 1 [[[[[1]]]]])
(f 0 [0 0 [0 [0]]])
(f 1 [-10 [1 [2 3 [4 5 [6 7 [8]]]]]])
(f 0 [1 2 [3 [4 5] 6] 7])])
; rank: 319 / 642282, 147 solved
(defn format-d-string [n s]
(remove clojure.string/blank? (->
(str "[" (apply str (interpose " " (take 25 s))) "]")
(clojure.string/replace #"[\-\d]+" #(str " " %1 " "))
(clojure.string/replace #"\[" #(str " " %1 " "))
(clojure.string/replace #"\]" #(str " " %1 " "))
(clojure.string/split #" "))))
#_(if (= "class clojure.lang.iterate" (str (type %)))
(into [] (take 10 s))
s)
#_(-> s
str
(clojure.string/replace #"[\-\d]+" #(str " " %1 " "))
(clojure.string/replace #"\[" #(str " " %1 " "))
(clojure.string/replace #"\]" #(str " " %1 " "))
(clojure.string/split #" "))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment