Compare commits
4 Commits
689fb894ad
...
fb3be522a0
Author | SHA1 | Date | |
---|---|---|---|
fb3be522a0 | |||
65e298c82c | |||
1dd77f9657 | |||
2dfc7ac5e4 |
57
rover.fnl
57
rover.fnl
@ -6,18 +6,31 @@
|
||||
(< 180 x) (wrap-longitude (- x 360))
|
||||
x))
|
||||
|
||||
(fn drive [{: x : y : direction : stopped &as r} distance]
|
||||
(if stopped
|
||||
(fn at-obstacle? [{: x : y : obstacles}]
|
||||
(any obstacles
|
||||
(fn [[ox oy]] (and (= ox x) (= oy y)))))
|
||||
|
||||
(fn lateral? [direction]
|
||||
(. {:w true :e true} direction))
|
||||
|
||||
(fn try-to-drive [{: x : y : direction} distance]
|
||||
(let [distance
|
||||
(if (lateral? direction)
|
||||
(/ distance (math.cos (/ (* math.pi y) 180)))
|
||||
distance)]
|
||||
(match direction
|
||||
:n {:y (+ y distance)}
|
||||
:s {:y (- y distance)}
|
||||
:w {:x (wrap-longitude (- x distance))}
|
||||
:e {:x (wrap-longitude (+ x distance))})))
|
||||
|
||||
(fn drive [r distance]
|
||||
(if r.stopped
|
||||
r
|
||||
(let [distance
|
||||
(if (. {:w true :e true} direction)
|
||||
(/ distance (math.cos (/ (* math.pi y) 180)))
|
||||
distance)]
|
||||
(match direction
|
||||
:n {:y (+ y distance)}
|
||||
:s {:y (- y distance)}
|
||||
:w {:x (wrap-longitude (- x distance))}
|
||||
:e {:x (wrap-longitude (+ x distance))}))))
|
||||
(let [next-move (merge r (try-to-drive r distance))]
|
||||
(if (at-obstacle? next-move)
|
||||
(merge r {:stopped [next-move.x next-move.y]})
|
||||
next-move))))
|
||||
|
||||
(fn turn-left [{: x : y : direction}]
|
||||
(if (= y 90)
|
||||
@ -43,22 +56,14 @@
|
||||
}
|
||||
(. direction))}))
|
||||
|
||||
(fn at-obstacle? [{: x : y : obstacles}]
|
||||
(any obstacles
|
||||
(fn [[ox oy]] (and (= ox x) (= oy y)))))
|
||||
|
||||
(fn command [rover string]
|
||||
(let [next-move
|
||||
(merge rover
|
||||
(match string
|
||||
:f (drive rover 1)
|
||||
:b (drive rover -1)
|
||||
:r (turn-right rover)
|
||||
:l (turn-left rover)
|
||||
_ (assert false (. "unrecognised command " string))))]
|
||||
(if (at-obstacle? next-move)
|
||||
(merge rover {:stopped [next-move.x next-move.y]})
|
||||
next-move)))
|
||||
(merge rover
|
||||
(match string
|
||||
:f (drive rover 1)
|
||||
:b (drive rover -1)
|
||||
:r (turn-right rover)
|
||||
:l (turn-left rover)
|
||||
_ (assert false (. "unrecognised command " string)))))
|
||||
|
||||
(fn fudge-for-pole [{: y &as r}]
|
||||
(if (= y 90)
|
||||
|
Loading…
Reference in New Issue
Block a user