From 09df1d724c323410d94e544edfeffe406866f173 Mon Sep 17 00:00:00 2001 From: Aleh Suprunovich Date: Sun, 8 Dec 2024 21:54:54 +0300 Subject: [PATCH] add helper module to work with mapped areas --- src/mapped_area.clj | 73 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/mapped_area.clj diff --git a/src/mapped_area.clj b/src/mapped_area.clj new file mode 100644 index 0000000..2efeb19 --- /dev/null +++ b/src/mapped_area.clj @@ -0,0 +1,73 @@ +(ns mapped-area + (:require [clojure.string :as string])) + +(def test-map +"abcd +efgh +ijkl") + +(defn read-mapped-area + {:test + #(do + (let [a (read-mapped-area test-map)] + (assert (= \i (get-in a [0 0]))) + (assert (= \f (get-in a [1 1]))) + (assert (= \a (get-in a [0 2]))) + (assert (nil? (get-in a [10 10]))) + (assert (nil? (get-in a [1 3]))) + (assert (= \h (get-in a [3 1])))))} + [s] + (let [ss (string/split-lines s) + lines (count ss)] + (->> + (reverse ss) + (apply interleave) + (partition lines) + (mapv vec)))) + +(defn print-mapped-area [a] + (let [lines (count (first a))] + (doall + (->> + (map reverse a) + (apply interleave) + (partition lines) + (map (partial apply str)) + (map println)))) + a) + +(comment + (print-mapped-area (read-mapped-area test-map)) + ) + +(defn- h + {:test #(do (assert (= (h (read-mapped-area test-map)) 3)))} + [a] + (count (first a))) + +(defn- w + {:test #(do (assert (= (w (read-mapped-area test-map)) 4)))} + [a] + (count a)) + +(defn mapped? + {:test + #(do + (let [a (read-mapped-area test-map)] + (assert (true? (mapped? a 1 1))) + (assert (true? (mapped? a [2 2]))) + (assert (false? (mapped? nil -1 0))) + (assert (false? (mapped? a 4 0))) + (assert (true? (mapped? a 3 1))) + (assert (false? (mapped? a 1 3)))))} + ([a [x y]] (mapped? a x y)) + ([a x y] + (and (>= x 0) + (>= y 0) + (< x (w a)) + (< y (h a))))) + +(test #'read-mapped-area) +(test #'h) +(test #'w) +(test #'mapped?)