122 lines
3.3 KiB
Fennel
122 lines
3.3 KiB
Fennel
(local { : view } (require :fennel))
|
|
(local { : merge } (require :lume))
|
|
|
|
;; who needs a test framework when you have lisp macros?
|
|
(macro expect [text actual expected]
|
|
`(let [actual# ,actual]
|
|
(or (match actual#
|
|
,expected true
|
|
_# false)
|
|
(assert false (view {
|
|
:text ,text
|
|
:expected ,expected
|
|
:actual actual#
|
|
})))))
|
|
|
|
(fn drive [{: x : y : direction} distance]
|
|
(match direction
|
|
:n {:y (+ y distance)}
|
|
:s {:y (- y distance)}
|
|
:w {:x (- x distance)}
|
|
:e {:x (+ x distance)}))
|
|
|
|
(fn turn-left [direction]
|
|
{:direction
|
|
(-> {
|
|
:n :w
|
|
:w :s
|
|
:s :e
|
|
:e :n
|
|
}
|
|
(. direction))})
|
|
|
|
(fn turn-right [direction]
|
|
{:direction
|
|
(-> {
|
|
:n :e
|
|
:e :s
|
|
:s :w
|
|
:w :n
|
|
}
|
|
(. direction))})
|
|
|
|
(fn command [rover string]
|
|
(merge rover
|
|
(match string
|
|
:f (drive rover 1)
|
|
:b (drive rover -1)
|
|
:r (turn-right rover.direction)
|
|
:l (turn-left rover.direction)
|
|
_ (assert false (. "unrecognised command " string)))))
|
|
|
|
(fn execute [rover cmds]
|
|
(accumulate [rover rover
|
|
_ c (ipairs cmds)]
|
|
(merge rover (command rover c))))
|
|
|
|
(fn rover [x y direction]
|
|
{: x
|
|
: y
|
|
: direction
|
|
})
|
|
|
|
(let [r (rover 7 15 :n)]
|
|
(expect
|
|
"rover knows (x,y) and the direction (N,S,E,W) it is facing"
|
|
r
|
|
{:x 7 :y 15 :direction :n}))
|
|
|
|
(let [r (rover 0 0 :n)]
|
|
(expect "The rover receives a character array of commands"
|
|
(execute r [:f :f :f])
|
|
{}))
|
|
|
|
(let [r (rover 0 0 :n)]
|
|
(expect "Moves north when pointing north and asked to move forward"
|
|
(execute r [:f]) {:x 0 :y 1 :direction :n}))
|
|
|
|
(let [r (rover 3 2 :s)]
|
|
(expect "Moves south when pointing south and asked to move forward"
|
|
(execute r [:f]) {:x 3 :y 1 :direction :s}))
|
|
|
|
(let [r (rover 0 0 :w)]
|
|
(expect "Moves west when pointing west and asked to move forward"
|
|
(execute r [:f]) {:x -1 :y 0 :direction :w}))
|
|
|
|
(let [r (rover 0 0 :e)]
|
|
(expect "Moves east when pointing east and asked to move forward"
|
|
(execute r [:f]) {:x 1 :y 0 :direction :e}))
|
|
|
|
(let [r (rover 1 1 :s)]
|
|
(expect "The rover acts on multiple commands"
|
|
(execute r [:f :f :f])
|
|
{:x 1 :y -2 :direction :s}))
|
|
|
|
(let [r (rover 3 2 :s)]
|
|
(expect "Moves north when pointing south and asked to move backward"
|
|
(execute r [:b]) {:x 3 :y 3 :direction :s}))
|
|
|
|
(let [r (rover 3 2 :e)]
|
|
(expect "Moves west when pointing east and asked to move backward"
|
|
(execute r [:b]) {:x 2 :y 2 :direction :e}))
|
|
|
|
(let [r (rover 2 4 :e)]
|
|
(expect "Rotates to south when pointing east and asked to turn right"
|
|
(execute r [:r]) {:x 2 :y 4 :direction :s}))
|
|
|
|
(let [r (rover 2 4 :s)]
|
|
(expect "Rotates to east when pointing south and asked to turn left"
|
|
(execute r [:l]) {:x 2 :y 4 :direction :e}))
|
|
|
|
(let [r (rover 2 4 :w)]
|
|
(expect "Rotates to north when pointing west and asked to turn right"
|
|
(execute r [:r]) {:x 2 :y 4 :direction :n}))
|
|
|
|
(let [r (rover 1 1 :s)]
|
|
(expect "Multiple commands are executed in the right order"
|
|
(execute r [:l :l :f :f :f :r :f])
|
|
{:x 2 :y 4 :direction :e}))
|
|
|
|
|
|
(print "OK")
|