-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
145 lines (119 loc) · 4.74 KB
/
util.js
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const database = require('./database');
const fs = require('fs');
function Vehicle(index) {
this.id = index;
this.posX = 0;
this.posY = 0;
this.rides = [];
this.step = 0;
}
function Ride() {
this.id = 0;
this.startPosX = 0;
this.startPosY = 0;
this.endPosX = 0;
this.endPosY = 0;
this.earliestStart = 0;
this.latestFinish = 0;
this.isTaken = false;
}
const calculateDistanceBetweenTwoPoints = (startX, startY, endX, endY) =>
Math.abs(startX - endX) + Math.abs(startY - endY);
const calculateDistanceFromVehicleToPosition = (vehicle, posX, posY) =>
calculateDistanceBetweenTwoPoints(vehicle.posX, vehicle.posY, posX, posY);
const calculateDistanceFromVehicleToRideStart = (vehicle, ride) =>
calculateDistanceFromVehicleToPosition(vehicle, ride.startPosX, ride.startPosY);
const calculateVehicleTimeToWaitForRide = (vehicle, ride) => {
const distanceToStart = calculateDistanceFromVehicleToRideStart(vehicle, ride);
const timeToWait = ride.earliestStart - (vehicle.step + distanceToStart);
return timeToWait <= 0 ? 0 : timeToWait;
};
const calculateRideDistance = ride =>
calculateDistanceBetweenTwoPoints(ride.startPosX, ride.startPosY, ride.endPosX, ride.endPosY);
const calculateTimeToCompleteRideForVehicle = (vehicle, ride) => {
const distanceToStart = calculateDistanceFromVehicleToRideStart(vehicle, ride);
const timeToWait = calculateVehicleTimeToWaitForRide(vehicle, ride);
const distance = calculateRideDistance(ride);
return distanceToStart + timeToWait + distance;
};
const willVehicleArriveOnTimeForBonus = (vehicle, ride) =>
vehicle.step + calculateDistanceFromVehicleToRideStart(vehicle, ride) <= ride.earliestStart;
const canRideBeCompletedOnTimeByVehicle = (vehicle, ride) => {
const timeItWillTake = calculateTimeToCompleteRideForVehicle(vehicle, ride);
const endTime = timeItWillTake + vehicle.step;
return endTime < ride.latestFinish;
};
const sortRideGoodnessObjectsByGoodness = (ride1, ride2) =>
(ride1.goodness >= ride2.goodness ? 1 : -1);
const assignRideToVehicle = (vehicle, ride) => {
const timeItWillTake = calculateTimeToCompleteRideForVehicle(vehicle, ride);
const indexOfRide = ride.id;
vehicle.posX = ride.endPosX;
vehicle.posY = ride.endPosY;
vehicle.rides.push(indexOfRide);
vehicle.step += timeItWillTake;
database.rides[indexOfRide].isTaken = true;
database.totalVehicleSteps += timeItWillTake;
database.averageVehicleSteps = database.totalVehicleSteps / database.vehiclesCount;
};
const isThereAPossibleRideForVehicle = vehicle =>
database.rides.filter(ride => !ride.isTaken).some(ride => canRideBeCompletedOnTimeByVehicle(vehicle, ride));
const isThereAVehicleBelowAvgStepsThatCanStillAcceptRide = () =>
database.vehicles.find(vehicle => vehicle.step <= database.averageVehicleSteps && isThereAPossibleRideForVehicle(vehicle));
const isThereAVehicleThatCanStillAcceptRide = () => {
const vehicleBelowAvg = isThereAVehicleBelowAvgStepsThatCanStillAcceptRide();
if (vehicleBelowAvg) {
return vehicleBelowAvg;
}
return database.vehicles.find(vehicle => isThereAPossibleRideForVehicle(vehicle));
};
const calculateHowGoodARideIs = (vehicle, ride) => {
if (!canRideBeCompletedOnTimeByVehicle(vehicle, ride)) {
return Number.MIN_VALUE;
}
const distanceToStart = calculateDistanceFromVehicleToRideStart(vehicle, ride);
const timeToWait = calculateVehicleTimeToWaitForRide(vehicle, ride);
const distance = calculateRideDistance(ride);
const rideBonus = willVehicleArriveOnTimeForBonus(vehicle, ride) ? database.bonus : 0;
let weightedTimeItWillTake = (distanceToStart * 2) + (timeToWait * 3);
let timeUntilEnd = ride.latestFinish - (distanceToStart + distance);
if (timeUntilEnd === 0) {
timeUntilEnd = 1;
}
if (weightedTimeItWillTake === 0) {
weightedTimeItWillTake = 1;
}
return (distance * (1 + rideBonus)) / (weightedTimeItWillTake * timeUntilEnd);
};
function rideToRideGoodnessObjectForVehicle(ride) {
if (ride.isTaken) {
return undefined;
}
const goodness = calculateHowGoodARideIs(this, ride);
return {
ride,
goodness,
};
}
const writeToFile = (path, value) =>
new Promise(resolve => fs.writeFile(path, value, () => resolve()));
module.exports = {
Vehicle,
Ride,
calculateDistanceBetweenTwoPoints,
calculateDistanceFromVehicleToPosition,
calculateDistanceFromVehicleToRideStart,
calculateVehicleTimeToWaitForRide,
calculateRideDistance,
calculateTimeToCompleteRideForVehicle,
willVehicleArriveOnTimeForBonus,
canRideBeCompletedOnTimeByVehicle,
sortRideGoodnessObjectsByGoodness,
assignRideToVehicle,
isThereAPossibleRideForVehicle,
isThereAVehicleBelowAvgStepsThatCanStillAcceptRide,
isThereAVehicleThatCanStillAcceptRide,
calculateHowGoodARideIs,
rideToRideGoodnessObjectForVehicle,
writeToFile,
};