-
Notifications
You must be signed in to change notification settings - Fork 1
/
day17_velocities.ts
116 lines (104 loc) · 3.21 KB
/
day17_velocities.ts
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
import fs from "fs";
import assert from "assert";
import _, { min } from "lodash";
import { GridSet } from "../aoc/utilities";
// #####
// INPUT
// #####
const parseInput = (puzzleInput: string): number[][] => {
// const [[xMin, xMax], [yMin, yMax]] = puzzleInput
return puzzleInput
.split("target area: ")
.slice(-1)[0]
.split(", ")
.map((value) => {
const range = value.split("=").slice(-1)[0];
const [min, max] = range.split("..").map(Number);
return [min, max];
});
};
const TEST_INPUT = `target area: x=20..30, y=-10..-5`;
const puzzleInput = fs
.readFileSync("2021/data/day17_input.txt")
.toString()
.trim();
// ########
// SOLUTION
// ########
// part 1
const part1 = (puzzleInput: string) => {
const [[xMin, xMax], [yMin, yMax]] = parseInput(puzzleInput);
const possibleStartVelocities: number[][] = [];
for (let x = 0; x <= 100; x++) {
for (let y = 0; y <= 500; y++) {
possibleStartVelocities.push([x, y]);
}
}
const highestElevation = possibleStartVelocities.map(
([xVelocity, yVelocity]) => {
let [x, y] = [0, 0];
let [currXVelocity, currYVelocity] = [xVelocity, yVelocity];
let hitTarget = false;
let highestY = 0;
while (true) {
[x, y] = [x + currXVelocity, y + currYVelocity];
if (y > highestY) highestY = y;
// did we hit the target or overshoot it?
if (x >= xMin && x <= xMax && y >= yMin && y <= yMax) {
hitTarget = true;
}
if (x > xMax || y < yMax) break;
// update velocity
if (currXVelocity < 0) currXVelocity += 1;
else if (currXVelocity == 0) currXVelocity += 0;
else currXVelocity -= 1;
currYVelocity -= 1;
}
return hitTarget ? highestY : -1;
},
);
return Math.max(...highestElevation);
};
assert(part1(TEST_INPUT) === 45);
console.time("part 1");
console.log(part1(puzzleInput));
console.timeEnd("part 1");
// part 2
const part2 = (puzzleInput: string) => {
const [[xMin, xMax], [yMin, yMax]] = parseInput(puzzleInput);
const allInitialVelocities = new GridSet();
// const possibleStartVelocities: number[][] = [[29, -10]];
const possibleStartVelocities: number[][] = [];
for (let x = -100; x <= 700; x++) {
for (let y = -1000; y <= 750; y++) {
possibleStartVelocities.push([x, y]);
}
}
possibleStartVelocities.forEach(([xVelocity, yVelocity]) => {
let [x, y] = [0, 0];
let [currXVelocity, currYVelocity] = [xVelocity, yVelocity];
let hitTarget = false;
while (true) {
[x, y] = [x + currXVelocity, y + currYVelocity];
// did we hit the target or overshoot it?
if (x >= xMin && x <= xMax && y >= yMin && y <= yMax) {
hitTarget = true;
}
if (x > xMax || y < yMin) break;
// update velocity
if (currXVelocity < 0) currXVelocity += 1;
else if (currXVelocity == 0) currXVelocity += 0;
else currXVelocity -= 1;
currYVelocity -= 1;
}
if (hitTarget) {
allInitialVelocities.add([xVelocity, yVelocity]);
}
});
return allInitialVelocities.size;
};
console.log(part2(TEST_INPUT));
assert(part2(TEST_INPUT) === 112);
console.time("part 2");
console.log(part2(puzzleInput));
console.timeEnd("part 2");