Skip to content

Instantly share code, notes, and snippets.

@nenorbot
Created June 30, 2015 06:21
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 nenorbot/e2f51ab9867ddf0ba4d0 to your computer and use it in GitHub Desktop.
Save nenorbot/e2f51ab9867ddf0ba4d0 to your computer and use it in GitHub Desktop.
reddit daily programmer challenge 221
(defn make-coords [word row row-inc col col-inc]
(map (fn [w row col] {:letter w :row row :col col})
word
(iterate #(+ % row-inc) row)
(iterate #(+ % col-inc) col)))
(defn make-next-direction [row col]
(println row col)
(let [modifier (if (= 0 (rand-int 2)) 1 -1)]
(cond
(= row 0) [modifier 0]
:else [0 1])))
(defn build-snake [words]
(reduce (fn [[acc [row row-inc col col-inc]] word]
(let [[row-inc col-inc] (make-next-direction row-inc col-inc)
coords (make-coords word (+ row row-inc) row-inc (+ col col-inc) col-inc)
last-coord (nth coords (dec (count coords)))]
[(concat acc coords) [(:row last-coord) row-inc (:col last-coord) col-inc]]))
[[] [0 0 0 0]]
(concat [(first words)] (map #(subs % 1) (rest words)))))
(defn print-snake [words]
(let [grid (first (snake words))]
(doseq [[_ row] (sort-by first (group-by :row grid))]
(loop [last-pos 0 cols (sort-by :col row)]
(if (seq? cols)
(let [curr (first row)
col (:col curr)]
(print (apply str (repeat (- col last-pos 1) " ")))
(print (:letter curr))
(recur col (rest row)))))
(println))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment