Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (def field-size 5)
- (def field-size-1 (- field-size 1))
- (def cell-count 25)
- (def field [[:S :S :SW :W :SW]
- [:E :SE :SE :SE :SW]
- [:S :N :N :W :SW]
- [:NE :NE :NW :SW :N ]
- [:E :NE :W :N :Z]])
- (def directions
- { :NW [-1 -1], :N [0 -1], :NE [1 -1]
- :W [-1 0], :E [1 0],
- :SW [-1 1], :S [0 1], :SE [1 1]})
- (defn in-field [x y]
- (and (>= x 0) (>= y 0)
- (< x field-size) (< y field-size)))
- (defn waylist [x y dir]
- (let [[dirx diry] (directions dir)]
- (when (in-field x y)
- (conj (waylist (+ x dirx) (+ y diry) dir) [x y]))))
- (defn can-visit [visits x y]
- (and (in-field x y)
- (= (get-in visits [x y]) 0)))
- (defn create-array [dim size value]
- (letfn [(ca [dim current]
- (if (zero? dim)
- current
- (ca (dec dim) (into [] (repeat size current)))))]
- (ca dim value)))
- (defn visit [visits x y n]
- (letfn [(vis [x y]
- (update-in visits [x y] (constantly n)))
- (unvis [x y]
- (update-in visits [x y] (constantly 0)))
- (special-case? []
- (and (= x 2) (= y 3) (not (= n 17))))
- (end-case? [] (and (= x field-size-1)
- (= y field-size-1)
- (= n cell-count)))]
- (when (can-visit visits x y)
- (let [new-visits (vis x y)]
- (cond (end-case?) new-visits
- (special-case?) nil
- :else (some (complement nil?)
- (map (fn [[nx ny]]
- (visit new-visits nx ny (+ 1 n)))
- (rest (waylist x y (get-in field x y))))))))))
- (def result (visit (create-array 2 5 0)
- 4 4 25))
- (def middle-sum (reduce + (result 2)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement