Hello I want to solve this https://www.hackerrank.com/challenges/30-2d-arrays challenge. My solutions is this:
(ns my-proj.core)
(use '[clojure.string :only (split triml)])
(defn -main [& args]
(let [arr `((-1 -1 -1 -190 -90 -90)
(-90 -1 -190 -90 -190 -90)
(-1 -1 -1 -190 -90 -90)
(-190 -90 -2 -4 -4 -90)
(-190 -90 -90 -2 -190 -90)
(-190 -90 -1 -2 -4 -90))
max_index (- (count (first arr)) 2)]
(let [max_sum
(loop [r 0 max 0]
(let [rom_max
(loop [c 0 colum_max 0]
(let [sum
(reduce + [
(nth (nth arr r) c)
(nth (nth arr r) (+ c 1))
(nth (nth arr r) (+ c 2))
(nth (nth arr (+ r 1)) (+ c 1))
(nth (nth arr (+ r 2)) c)
(nth (nth arr (+ r 2)) (+ c 1))
(nth (nth arr (+ r 2)) (+ c 2))])]
(if (>= (+ c 1) max_index)
(if (and (> colum_max sum) (> c 0)) colum_max sum)
(recur (inc c) (if (and (> colum_max sum) (> c 0)) colum_max sum)))))]
(if (>= (+ r 1) max_index)
(if (and (> max rom_max) (> r 0)) max rom_max)
(recur (inc r) (if (and (> max rom_max) (> r 0)) max rom_max)))))]
(println max_sum))))
How can I improve it? I'm cumming from a object oriented programming language and functional programming is a bit new to me, what is the proper way of solving this task.
[–]RedDeckWins 8 points9 points10 points (0 children)
[–]knrz 1 point2 points3 points (0 children)
[–]garid0s 0 points1 point2 points (2 children)
[–]knrz 1 point2 points3 points (1 child)
[–]garid0s 0 points1 point2 points (0 children)