forked from marijnh/Eloquent-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_2_robot_efficiency.js
More file actions
26 lines (23 loc) · 865 Bytes
/
07_2_robot_efficiency.js
File metadata and controls
26 lines (23 loc) · 865 Bytes
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
function lazyRobot({place, parcels}, route) {
if (route.length == 0) {
// Describe a route for every parcel
let routes = parcels.map(parcel => {
if (parcel.place != place) {
return {route: findRoute(roadGraph, place, parcel.place),
pickUp: true};
} else {
return {route: findRoute(roadGraph, place, parcel.address),
pickUp: false};
}
});
// This determines the precedence a route gets when choosing.
// Route length counts negatively, routes that pick up a package
// get a small bonus.
function score({route, pickUp}) {
return (pickUp ? 0.5 : 0) - route.length;
}
route = routes.reduce((a, b) => score(a) > score(b) ? a : b).route;
}
return {direction: route[0], memory: route.slice(1)};
}
runRobotAnimation(VillageState.random(), lazyRobot, []);