-
Notifications
You must be signed in to change notification settings - Fork 1
/
ch8p2_robot_in_a_grid.clj
50 lines (39 loc) · 1.13 KB
/
ch8p2_robot_in_a_grid.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env bb
(require '[clojure.test :as t])
(def robot-in-a-grid
(memoize
(fn [grid row column] (let [row-count (-> grid count)
column-count (-> grid first count)
open? (get-in grid [row column])]
(cond
(<= row-count row)
false
(<= column-count column)
false
(false? open?)
false
(and (-> row-count dec (= row))
(-> column-count dec (= column)))
[]
:default
(or (when-let [go-down (robot-in-a-grid grid (inc row) column)]
(cons :down go-down))
(when-let [go-right (robot-in-a-grid grid row (inc column))]
(cons :right go-right))
false))))))
(t/deftest test
(t/are [vs expected] (= expected (robot-in-a-grid vs 0 0))
[[true]] []
[[false]] false
[[true true]
[true true]] [:down :right]
[[true true]
[true false]] false
[[false true]
[true true]] false
[[true false]
[true true]] [:down :right]
[[true true]
[false true]] [:right :down]))
(t/run-tests *ns*)
(println (java.util.Date.))