My main concern is how messy the
min-number-of-slices function is. I would appreciate help finding good ways to clean that up since it has a lot of nesting/scope.
This is my first ever Clojure program, so I know this is probably not idiomatic Clojure, nor is it neat, nor probably fast, either. Please give as much feedback as you would like on my code (especially regarding stuff that's bad about it).
The challenge is, given a list of 2D dimensions for pieces of bread, find the minimum number of perfect squares you could cut out of each piece without wasting any bread. https://www.hackerrank.com/challenges/restaurant
(ns clojure-solution.core) (require '[clojure.string :as string]) (defn parse-int [s] (Integer. s)) (defn parse-line [s] (map parse-int (string/split s #" "))) (defn get-loaf-dimensions  (let [numLoaves (parse-int (read-line))] (map #(%1) (repeat numLoaves (comp parse-line read-line))))) (def squares (map #(* % %) (range 1 1001))) (defn min-number-of-slices [dims] (let [l (first dims) b (second dims) area (* l b)] (letfn [(perfect-slice-dimension? [slice-area] (= 0 (+ (mod area slice-area) (mod b (int (Math/sqrt slice-area))) (mod l (int (Math/sqrt slice-area))))))] (let [largest-square (last (filter perfect-slice-dimension? (take (min l b) squares)))] (int (/ area largest-square)))))) (defn -main [& args] (dorun (map println (map min-number-of-slices (get-loaf-dimensions)))))