Skip to content

Instantly share code, notes, and snippets.

@seltzer1717
Created June 24, 2021 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seltzer1717/f4e230feab0c1837e1d688a3d74543d6 to your computer and use it in GitHub Desktop.
Save seltzer1717/f4e230feab0c1837e1d688a3d74543d6 to your computer and use it in GitHub Desktop.
(ns cloud.seltzer1717.s3.apartment-test
(:require [clojure.test :as test]
[cloud.seltzer1717.s3.apartment :as apt]))
(test/with-test
(def sample-blocks
[{:gym false :school true :store false}
{:gym true :school false :store false}
{:gym true :school true :store false}
{:gym false :school true :store false}
{:gym false :school true :store true}])
(def sample-requirements
[:gym :school :store])
(test/is (= 3 (apt/distance 0 3)))
(test/is (= 3 (apt/distance 3 0)))
(test/is (= [1 2] (apt/indexes-by-type sample-blocks :gym)))
(test/is (= [0 2 3 4] (apt/indexes-by-type sample-blocks :school)))
(test/is (= 1 (apt/min-distance-by-app-ind-type sample-blocks 0 :gym)))
(test/is (= 1 (apt/min-distance-by-app-ind-type sample-blocks 1 :school)))
(test/is (= 4 (apt/min-distance-by-app-ind-type sample-blocks 0 :store)))
(test/is (= 4 (apt/max-distance-by-app-ind sample-blocks 0 sample-requirements)))
(test/is (= 3 (apt/max-distance-by-app-ind sample-blocks 1 sample-requirements)))
(test/is (= 1 (apt/max-distance-by-app-ind sample-blocks 3 sample-requirements)))
(test/is (= 2 (apt/max-distance-by-app-ind sample-blocks 4 sample-requirements)))
(test/is (= [3 1] (apt/best-apartment sample-blocks sample-requirements)))
(test/is (= [{:gym false :school true :store false :winner false}
{:gym true :school false :store false :winner false}
{:gym true :school true :store false :winner false}
{:gym false :school true :store false :winner true}
{:gym false :school true :store true :winner false}]
(apt/show-best sample-blocks sample-requirements)))
)
(ns cloud.seltzer1717.apartment)
(defn distance
[apt-ind blk-ind]
(Math/abs (- blk-ind apt-ind)))
(defn indexes-by-type
[blocks type]
(->> blocks
(map-indexed (fn [ind block] (if (type block) ind nil)))
(filter #(not (nil? %)))
vec))
(defn min-distance-by-app-ind-type
[blocks apt-ind type]
(->> type
(indexes-by-type blocks)
(map (partial distance apt-ind))
(apply min)))
(defn max-distance-by-app-ind
[blocks apt-ind requirements]
(apply max
(map (partial min-distance-by-app-ind-type blocks apt-ind)
requirements)))
(defn best-apartment
[blocks requirements]
(reduce (fn [[bind bmaxmin :as best] [ind maxmin :as current]] (if (> bmaxmin maxmin) current best))
(map-indexed (fn [ind block]
[ind (max-distance-by-app-ind blocks ind requirements)])
blocks)))
(defn show-best
[blocks requirements]
(let [[bind bmaxmin] (best-apartment blocks requirements)
blk-cnt (count blocks)]
(loop [output blocks
ind 0]
(if (<= 0 ind (dec blk-cnt))
(let [winner (= ind bind)]
(recur (update-in output [ind :winner] (constantly winner))
(inc ind)))
output))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment