generated from mariotacke/template-advent-of-code
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpart1.js
42 lines (32 loc) · 708 Bytes
/
part1.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
const simulateY = (velocity, min, max) => {
let dy = 0;
let maxY = -Infinity;
while (dy >= max) {
dy += velocity;
velocity--;
if (dy > maxY) {
maxY = dy;
}
if (velocity < 0 && dy >= min && dy < max) {
return maxY;
}
}
};
const searchY = (min, max) => {
let velocity = 1;
let counter = 0;
let maxY = -Infinity;
while (counter++ < 1000) {
const height = simulateY(velocity, min, max);
if (height && height > maxY) {
maxY = height;
}
velocity++;
}
return maxY;
};
module.exports = (input) => {
const [minY, maxY] = /y=(-?\d+)\.\.(-?\d+)/
.exec(input).slice(1).map((value) => +value);
return searchY(minY, maxY);
};