clojurecademy / 4Clojure

4Clojure problems with a few differences
https://clojurecademy.com/courses/17592186045426/learn/overview
8 stars 2 forks source link

Unexpected timeout on Code Kata: Wonderlands / 3 #19

Open oliverjensen-wk opened 6 years ago

oliverjensen-wk commented 6 years ago

In the tiny-maze problem, I'm getting a code timeout that I don't understand. My submission fails the second test (4x4 maze) with the following error:

Execution Time Out! Your code execution time took more than 2.5 seconds.

However, executing the identical test in a local REPL returns instantly. In don't understand where the extra delay is coming from.

For reference, my code is as follows:

(ns tiny-maze.solver)

(defn find-start [maze]
  (let [width (count (first maze))
        flat  (flatten maze)
        idx   (.indexOf flat :S)]
    [(quot idx width) (rem idx width)]
    ))

(defn get-loc [row col maze]
  (nth (nth maze row) col))

(defn mark-place [row col maze]
  (partition (count (first maze))
    (for [r (range (count maze))
          c (range (count (first maze)))]
      (if (and (= r row) (= c col)) :x (get-loc r c maze)))))

(defn solve-maze-helper [cr cc maze]
  (if (or (< cr 0) (< cc 0) (>= cr (count maze)) (>= cc (count (first maze))))
    false
    (let [current   (get-loc cr cc maze)
          progress  (mark-place cr cc maze)]
      (if (= current :E) progress
        (if (and (not= current 0) (not= current :S)) false
          (or 
            (solve-maze-helper (inc cr) cc progress)
            (solve-maze-helper (dec cr) cc progress)
            (solve-maze-helper cr (inc cc) progress)
            (solve-maze-helper cr (dec cc) progress)))))))

(defn solve-maze 
  [maze]
  (let [[row col] (find-start maze)]
    (solve-maze-helper row col maze)))
ertugrulcetin commented 6 years ago

@oliverjensen-wk thanks for reaching out! There is a time limitation for security reason which is 2.5 seconds so you need to optimize your code for this problem, sorry about that :(