-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathday17.js
88 lines (78 loc) · 2.31 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict';
const fs = require('fs');
const cl = console.log;
fs.readFile('day17.txt', 'utf-8', (err, input) => {
if (err) throw err;
const [_, left, right, bottom, top] = input.match(
/target area: x=(\d+)..(\d+), y=(-?\d+)..(-?\d+)/).map(Number);
const target = { left, right, bottom, top };
let [apex, options] = trickShot(target);
cl(apex);
cl(options);
});
function trickShot(target, cocky) {
const probes = initialProbes(target);
const onTargetProbes = [...filter(probes, isOnTarget(target))];
const apexes = [...onTargetProbes].map(probe => probe.apex);
return [Math.max(...apexes), onTargetProbes.length];
}
function isOnTarget(target) {
return probe => {
const trajStart = takeWhile(trajectory(probe), inScope(target));
const hits = filter(trajStart, hitsTarget(target));
for (let _ of hits) {
return true;
}
return false;
};
}
function* initialProbes(target) {
// When shot upwards the y values while ascending are revisited when
// falling, so it always revisits y=0. So if |dy| > |bottom| we can be
// sure it'll be below the target on the next step and miss the target.
const xMax = target.right;
const yMax = -target.bottom;
for (let dx = 1; dx <= xMax; dx++) {
for (let dy = -yMax; dy <= yMax; dy++) {
yield { x: 0, y: 0, dx, dy, apex: 0 };
}
}
}
function update(probe) {
probe.x = probe.x += probe.dx;
probe.y = probe.y += probe.dy;
probe.dx = probe.dx === 0 ? 0 : probe.dx - 1;
probe.dy -= 1;
probe.apex = Math.max(probe.apex, probe.y);
}
function* trajectory(probe) {
for (; ; update(probe)) {
yield probe;
}
}
function inScope(target) {
return probe => probe.x <= target.right && probe.y >= target.bottom;
}
function hitsTarget(target) {
return probe =>
probe.x >= target.left
&& probe.y <= target.top
&& probe.x <= target.right
&& probe.y >= target.bottom;
}
function* takeWhile(generator, predicate) {
for (let item of generator) {
if (predicate(item)) {
yield item;
} else {
break;
}
}
}
function* filter(generator, predicate) {
for (let item of generator) {
if (predicate(item)) {
yield item;
}
}
}