-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolver.js
50 lines (41 loc) · 1.96 KB
/
solver.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
const util = require('./util');
const database = require('./database');
const readline = require('readline');
const fs = require('fs');
const start = inputFileName => new Promise((resolve) => {
let lineCounterIn = 0;
const rd = readline.createInterface({
input: fs.createReadStream(`./input/${inputFileName}.in`),
});
rd.on('line', (line) => {
if (lineCounterIn === 0) {
const params = line.split(' ').map(string => +string);
[, , database.vehiclesCount, database.ridesCount, database.bonus] = params;
database.vehicles = new Array(database.vehiclesCount).fill(undefined).map((element, index) => new util.Vehicle(index));
}
if (lineCounterIn > 0 && lineCounterIn <= database.ridesCount) {
const newRide = new util.Ride();
const params = line.split(' ').map(string => +string);
[newRide.startPosX, newRide.startPosY, newRide.endPosX, newRide.endPosY, newRide.earliestStart, newRide.latestFinish] = params;
newRide.id = lineCounterIn - 1;
database.rides.push(newRide);
if (lineCounterIn === database.ridesCount) {
let vehicleToAssign = util.isThereAVehicleThatCanStillAcceptRide();
while (vehicleToAssign) {
let rideGoodnessObjectArray = [];
rideGoodnessObjectArray = database.rides.filter(ride => !ride.isTaken).map(util.rideToRideGoodnessObjectForVehicle, vehicleToAssign);
rideGoodnessObjectArray = rideGoodnessObjectArray.sort(util.sortRideGoodnessObjectsByGoodness).reverse();
util.assignRideToVehicle(vehicleToAssign, rideGoodnessObjectArray[0].ride);
vehicleToAssign = util.isThereAVehicleThatCanStillAcceptRide();
}
const fileContent = database.vehicles.reduce((temp, vehicle) =>
`${temp}${vehicle.rides.length} ${vehicle.rides.join(' ')}\n`, '');
util.writeToFile(`./output/${inputFileName}.out`, fileContent).then(() => resolve());
}
}
lineCounterIn++;
});
});
module.exports = {
start,
};