100 lines
2.1 KiB
Clojure
100 lines
2.1 KiB
Clojure
(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)))))
|
|
|
|
(defn mapped-area-indexed-seq [a]
|
|
(for [x (range (w a))
|
|
y (range (h a))]
|
|
(vector [x y] (get-in a [x y]))))
|
|
|
|
(def test-map-num
|
|
"1234
|
|
5678")
|
|
|
|
(defn parse-elements
|
|
{:test
|
|
#(do
|
|
(let [a (parse-elements
|
|
(read-mapped-area test-map-num))]
|
|
(assert (number? (get-in a [0 0])))
|
|
(assert (== 5 (get-in a [0 0])))
|
|
(assert (== 1 (get-in a [0 1])))))}
|
|
([a] (parse-elements a (comp parse-long str)))
|
|
([a parse-fn]
|
|
(reduce
|
|
(fn [coll [coord value]]
|
|
(assoc-in coll coord (parse-fn value)))
|
|
[[]]
|
|
(mapped-area-indexed-seq a))))
|
|
|
|
(test #'read-mapped-area)
|
|
(test #'h)
|
|
(test #'w)
|
|
(test #'mapped?)
|
|
(test #'parse-elements)
|