rover/rover.fnl

56 lines
1.5 KiB
Plaintext
Raw Normal View History

2023-01-22 14:59:29 +00:00
(local { : view } (require :fennel))
(macro expect [text actual expected]
`(let [actual# ,actual]
(or (match actual#
,expected true
_# false)
(assert false (view {
:text ,text
:expected ,expected
:actual actual#
})))))
(fn command [rover commands]
2023-01-22 17:15:23 +00:00
(match rover.direction
:n {:x rover.x :y (+ rover.y 1) :direction :n}
:s {:x rover.x :y (- rover.y 1) :direction :s}
:w {:x (- rover.x 1) :y rover.y :direction :w}
:e {:x (+ rover.x 1) :y rover.y :direction :e}))
2023-01-22 14:59:29 +00:00
(fn rover [x y direction]
{: x
: y
: direction
: command
})
(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}))
2023-01-22 14:59:29 +00:00
(let [r (rover 0 0 :n)]
(expect "The rover receives a character array of commands"
(r:command [:f :f :f])
{}))
(let [r (rover 0 0 :n)]
2023-01-22 17:21:34 +00:00
(expect "Moves north when pointing north and asked to move forward"
(r:command [:f]) {:x 0 :y 1 :direction :n}))
(let [r (rover 3 2 :s)]
2023-01-22 17:21:34 +00:00
(expect "Moves south when pointing south and asked to move forward"
(r:command [:f]) {:x 3 :y 1 :direction :s}))
2023-01-22 17:21:34 +00:00
(let [r (rover 0 0 :w)]
(expect "Moves west when pointing west and asked to move forward"
(r:command [:f]) {:x -1 :y 0 :direction :w}))
2023-01-22 17:15:23 +00:00
2023-01-22 17:21:34 +00:00
(let [r (rover 0 0 :e)]
(expect "Moves east when pointing east and asked to move forward"
(r:command [:f]) {:x 1 :y 0 :direction :e}))
(print "OK")