Skip to content

Instantly share code, notes, and snippets.

@anon767
Created August 31, 2018 12:06
Show Gist options
  • Save anon767/4e7f0b2863e26e8dfd38477d6a7cb881 to your computer and use it in GitHub Desktop.
Save anon767/4e7f0b2863e26e8dfd38477d6a7cb881 to your computer and use it in GitHub Desktop.
(defn sum [oneDimArr]
(reduce + oneDimArr)
)
(defn singleHourGlassSum [arr , i , j]
(sum
(vector
(get-in arr [i j])
(get-in arr [i (+ j 1)])
(get-in arr [i (+ j 2)])
(get-in arr [(+ i 1) (+ j 1)])
(get-in arr [(+ i 2) j])
(get-in arr [(+ i 2) (+ j 1)])
(get-in arr [(+ i 2) (+ j 2)])
)
)
)
; Complete the hourglassSum function below.
(defn hourglassSum [arr]
(let [x (atom [])]
(dotimes [i 4]
(dotimes [j 4]
(swap! x #(vec (concat % (vector (singleHourGlassSum arr i j)))))
)
)
(apply max @x)
)
)
(def fptr (get (System/getenv) "OUTPUT_PATH"))
(def arr [])
(doseq [_ (range 6)]
(def arr (conj arr (vec (map #(Integer/parseInt %) (clojure.string/split (read-line) #" ")))))
)
(def result (hourglassSum arr))
(spit fptr (str result "\n") :append true)
@c-goetz
Copy link

c-goetz commented Aug 31, 2018

(def arr
  (loop [acc []
         i 0]
    (if (< i 6)
      (recur (conj acc (parse-line (read-line))) (inc i))
      acc)))
(defn hourglass-sum [arr]
  (apply max (for [i (range 4)
                   j (range 4)]
               (single-sum arr i j))))
(def hourglass-offsets
  [[0 0] [0 1] [0 2]
         [1 1]
   [2 0] [2 1] [2 2]])

(defn single-hourglass [arr i j]
  (sum (mapv (fn [[y x]] (get-in arr y x)) hourglass-offsets)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment