-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday17.js
69 lines (62 loc) · 1.3 KB
/
day17.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
const fs = require("fs");
const line = fs
.readFileSync("day17.txt", { encoding: "utf-8" }) // read day??.txt content
.trim();
const target = line.match(
/target area: x=(?<xMin>-?\d+)..(?<xMax>-?\d+), y=(?<yMin>-?\d+)..(?<yMax>-?\d+)/
).groups;
function simulate({ vx, vy, target }) {
const steps = [];
let x = 0;
let y = 0;
while (x < target.xMax && y > target.yMin) {
steps.push({ x, y });
x += vx;
y += vy;
if (vx > 0) {
vx--;
}
if (vx < 0) {
vx++;
}
vy--; //gravity
if (
x >= target.xMin &&
x <= target.xMax &&
y >= target.yMin &&
y <= target.yMax
) {
steps.push({ x, y });
return steps;
}
}
}
function part1() {
let maxY = 0;
for (let vx = 0; vx < target.xMax; vx++) {
for (let vy = 1000; vy >= 0; vy--) {
const steps = simulate({ vx, vy, target });
if (steps) {
let max = Math.max(...steps.map((p) => p.y));
if (max > maxY) {
maxY = max;
}
}
}
}
console.log(maxY);
}
part1();
function part2() {
let total = 0;
for (let vx = 1; vx <= target.xMax; vx++) {
for (let vy = 1000; vy >= target.yMin; vy--) {
const steps = simulate({ vx, vy, target });
if (steps) {
total++;
}
}
}
console.log(total);
}
part2();