-
Notifications
You must be signed in to change notification settings - Fork 0
/
11.js
67 lines (62 loc) · 1.54 KB
/
11.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
const fs = require("fs");
const readline = require("readline");
let values = [];
let errorMargin = 1
async function processLineByLine() {
const fileStream = fs.createReadStream("input_for_11_12.txt");
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
rl.on("line", (word) => {
values.push(
word
.substring(10)
.trim()
.replace(/\s+/gm, " ")
.split(" ")
.map((e) => parseInt(e))
);
});
rl.on("close", () => {
for (let i = 0; i < values[0].length; i++) {
let maxTime = values[0][i];
let distanceGoal = values[1][i];
let possibleWins = []
for (
let held = 0;
held < maxTime;
held++
) {
let win = calcRace(held,distanceGoal,maxTime)
if(win){
possibleWins.push(held)
}
}
console.log("possibleWins",possibleWins)
errorMargin *= possibleWins.length
// console.log("distanceGoal",distanceGoal)
}
console.log(errorMargin)
});
}
processLineByLine();
function calcRace(held, distanceGoal, timeLimit) {
let time = 0;
let distance = 0;
// console.log("held", held);
// console.log("time", time);
// console.log("distanceGoal", distanceGoal);
// console.log("distance", distance);
while (distance <= distanceGoal && time < timeLimit) {
time++;
if (held < time) {
distance += held;
}
if (distance > distanceGoal) {
return true;
}
// console.log(time, "ms elapsed", distance, "mm moved");
}
return false
}