-
Notifications
You must be signed in to change notification settings - Fork 0
/
monkeys.js
127 lines (94 loc) · 2.8 KB
/
monkeys.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
/**
* Problème : le pont du singe.
*/
const fs = require('fs');
const lineStartMonkey = 6;
const numberOfLinesPerMonkey = 2;
// Pont
// {
// longueur: number;
// capacite: number;
// }
// N : nombre de singes
// Monkeys
// [
// {
// poids: number;
// vitesse: number;
// }
// ]
// returns: { pont, n, monkeys }
function readFile(path) {
const data = fs.readFileSync(path, 'utf8').split('\n');
const longueur = parseInt(data[1]);
const capacite = parseInt(data[3]);
let n = parseInt(data[5]);
let pont = { longueur, capacite };
let monkeys = [];
for (let i = lineStartMonkey; i < data.length - numberOfLinesPerMonkey; i += numberOfLinesPerMonkey) {
const monkey = {
poids: parseInt(data[i]),
vitesse: parseInt(data[i + 1]),
};
monkeys.push(monkey);
};
return {
pont, n, monkeys,
gameName: path,
}
}
function weightOnBridge(monkeysOnBridge) {
return monkeysOnBridge.reduce((acc, monkey) => acc + monkey.poids, 0);
}
function canMonkeyEnterBridge(monkey, monkeysOnBridge, pont) {
return weightOnBridge(monkeysOnBridge) + monkey.poids <= pont.capacite
}
function calculateMonkeyTravelTime(monkey, longueurPont) {
return longueurPont / monkey.vitesse;
}
function slowestExitTime(monkeysOnBridge) {
return monkeysOnBridge.reduce((acc, monkey) => Math.max(acc, monkey.exitTime), 0);
}
// returns time at which the last monkey has crossed the bridge
function computeGame(game) {
let seconds = 0;
let monkeysOnBridge = [];
let monkeysWaiting = game.monkeys;
while (monkeysWaiting.length > 0) {
// soit j'entre car c'est dispo
if (canMonkeyEnterBridge(monkeysWaiting[0], monkeysOnBridge, game.pont)) {
const monkey = monkeysWaiting.shift();
const indexOnBridge = monkeysOnBridge.length;
monkeysOnBridge.push(monkey);
monkey.exitTime = Math.max(
(seconds + calculateMonkeyTravelTime(monkey, game.pont.longueur)),
slowestExitTime(monkeysOnBridge.slice(0, indexOnBridge))
);
} else {
// c'est l'heure de faire sortir le prochaine à sortir.
const monkey = monkeysOnBridge.shift();
seconds = monkey.exitTime;
}
}
return monkeysOnBridge.pop().exitTime;
}
// returns: {
// gameName: string;
// strategyName: string;
// traveltime: number;
// runtime: number;
// }
function runGame(game) {
const startTime = Date.now();
const traveltime = computeGame(game);
const endTime = Date.now();
return {
strategyName: game.strategyName,
gameName: game.gameName,
traveltime,
runtime: endTime - startTime,
};
}
module.exports = {
readFile, runGame,
}