rover/rover.fnl

128 lines
3.6 KiB
Plaintext
Raw Normal View History

2023-01-22 14:59:29 +00:00
(local { : view } (require :fennel))
2023-01-22 17:50:48 +00:00
(local { : merge } (require :lume))
2023-01-22 14:59:29 +00:00
2023-01-22 17:54:03 +00:00
;; 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))})
2023-01-22 18:03:16 +00:00
(fn command [rover string]
2023-01-22 18:11:43 +00:00
(merge rover
(match string
:f (drive rover 1)
:b (drive rover -1)
:r (turn-right rover.direction)
:l (turn-left rover.direction)
2023-01-22 18:11:43 +00:00
_ (assert false (. "unrecognised command " string)))))
2023-01-22 17:54:03 +00:00
(fn execute [rover cmds]
(accumulate [rover rover
_ c (ipairs cmds)]
(merge rover (command rover c))))
2023-01-22 14:59:29 +00:00
(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}))
2023-01-22 14:59:29 +00:00
(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)]
2023-01-22 17:21:34 +00:00
(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)]
2023-01-22 17:21:34 +00:00
(expect "Moves south when pointing south and asked to move forward"
(execute r [: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"
(execute r [: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"
(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}))
2023-01-22 18:42:38 +00:00
(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 0 :e)]
2023-01-22 18:42:38 +00:00
(expect "Moves west when pointing east and asked to move backward"
(execute r [:b]) {:x 2 :y 0 :direction :e}))
2023-01-22 18:42:38 +00:00
2023-01-22 18:03:16 +00:00
(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}))
2023-01-22 18:03:16 +00:00
2023-01-22 18:06:41 +00:00
(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}))
2023-01-22 18:06:41 +00:00
2023-01-22 18:03:16 +00:00
(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}))
2023-01-22 18:03:16 +00:00
(let [r (rover 1 1 :s)]
(expect "Multiple commands are executed in the right order"
(execute r [:l :l :f :f :f :r :f :b])
{:x 1 :y 4 :direction :e}))
2023-01-22 19:05:35 +00:00
"if we travel west past x=-180, x becomes positive 180"
"if we travel east past x=180, x becomes -180"
"if we travel east/west at a non-zero latitude, we don't move a full unit"
"if we arrive at the north pole, we are {:y 90 :direction :s}"
"if we arrive at the north pole and turn 90 degrees left, x = x - 90"
(print "OK")