Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save flengyel/1446335 to your computer and use it in GitHub Desktop.
Save flengyel/1446335 to your computer and use it in GitHub Desktop.
;; flengyel's solution to Sequs Horribilis
;; https://4clojure.com/problem/112
(fn [n s]
(letfn [(sequs [n s]
(loop [acc 0 v [] s s]
(if (or (empty? s) (< n acc))
[acc v]
(let [elt (first s)]
(if (vector? elt)
(let [[m t] (sequs (- n acc) elt)
newacc (+ acc m)]
(recur newacc (if (empty? t) v (conj v t)) (rest s)))
(let [newacc (+ acc elt)]
(recur newacc (if (< n newacc) v (conj v elt)) (rest s))))))))]
(second (sequs n s))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment