Skip to content

Commit 3c89895

Browse files
committed
bruteforced 17b; 17a was calced in mind
1 parent b5d1c7a commit 3c89895

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

.idea/codeStyles/codeStyleConfig.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2021/src/2021-17.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
type range = [number, number];
2+
// type Coords = {x: number, y: number};
3+
type Area = {x: range, y: range}
4+
enum HitResult { miss, hit, over }
5+
6+
const input17: Area = {x: [144, 178], y: [-100, -76]};
7+
const input17_sample: Area = {x: [20, 30], y: [-10, -5]};
8+
9+
function checkHit(area: Area, x: number, y: number): HitResult {
10+
if (x >= area.x[0] && x <= area.x[1] && y >= area.y[0] && y <= area.y[1]) return HitResult.hit;
11+
if (x > area.x[1] || y < area.y[0]) return HitResult.over;
12+
return HitResult.miss;
13+
}
14+
15+
function maxStepsV(r: number): number {
16+
let s = 1;
17+
while (s < r) { r -= s; s++; }
18+
return s;
19+
}
20+
21+
function calcThrow(area: Area, vx: number, vy: number): HitResult {
22+
let x = 0;
23+
let y = 0;
24+
let res = HitResult.miss;
25+
26+
while (res === HitResult.miss) {
27+
x += vx;
28+
y += vy--;
29+
if (vx > 0) vx--;
30+
res = checkHit(area, x, y);
31+
}
32+
33+
return res;
34+
}
35+
36+
// const sampleCheckValuesRaw = [23,-10, 25,-9, 27,-5, 29,-6, 22,-6, 21,-7, 9,0, 27,-7, 24,-5, 25,-7 , 26,-6 , 25,-5 , 6,8 , 11,-2 , 20,-5 , 29,-10, 6,3 , 28,-7, 8,0 , 30,-6 , 29,-8 , 20,-10, 6,7 , 6,4 , 6,1 , 14,-4 , 21,-6, 26,-10 , 7,-1 , 7,7 , 8,-1 , 21,-9 , 6,2 , 20,-7 , 30,-10, 14,-3, 20,-8 , 13,-2 , 7,3 , 28,-8 , 29,-9 , 15,-3 , 22,-5 , 26,-8 , 25,-8, 25,-6 , 15,-4 , 9,-2 , 15,-2 , 12,-2 , 28,-9 , 12,-3 , 24,-6 , 23,-7, 25,-10 , 7,8 , 11,-3 , 26,-7 , 7,1 , 23,-9 , 6,0 , 22,-10, 27,-6, 8,1 , 22,-8 , 13,-4 , 7,6 , 28,-6 , 11,-4 , 12,-4 , 26,-9 , 7,4, 24,-10 , 23,-8 , 30,-8 , 7,0 , 9,-1 , 10,-1 , 26,-5 , 22,-9 , 6,5, 7,5 , 23,-6 , 28,-10, 10,-2 , 11,-1 , 20,-9 , 14,-2 , 29,-7 , 13,-3, 23,-5 , 24,-8 , 27,-9 , 30,-7 , 28,-5 , 21,-10, 7,9 , 6,6 , 21,-5, 27,-10, 7,2 , 30,-9 , 21,-8 , 22,-7 , 24,-9 , 20,-6 , 6,9 , 29,-5, 8,-2 , 27,-8 , 30,-5 , 24,-7];
37+
// const sampleCheckValues: { [id: string]: number } = {};
38+
// while (sampleCheckValuesRaw.length > 0) sampleCheckValues[`${<number> sampleCheckValuesRaw.shift()}, ${<number> sampleCheckValuesRaw.shift()}`] = 1;
39+
40+
function B(area: Area): number {
41+
// calc min-max initial velocity ranges
42+
const mmvx: range = [
43+
maxStepsV(area.x[0]), // max number of steps to reach
44+
area.x[1] // one max step forward
45+
];
46+
47+
const mmvy: range = [
48+
area.y[0], // one max step down
49+
-area.y[0] // highest throw
50+
];
51+
52+
console.log(mmvx, mmvy);
53+
54+
let n = 0;
55+
for (let vx = mmvx[0]; vx <= mmvx[1]; vx++)
56+
for (let vy = mmvy[0]; vy <= mmvy[1]; vy++)
57+
if (calcThrow(area, vx, vy) === HitResult.hit) {
58+
// console.log(vx, vy);
59+
// delete sampleCheckValues[`${vx}, ${vy}`];
60+
n++;
61+
}
62+
63+
// console.log(Object.keys(sampleCheckValues));
64+
return n;
65+
}
66+
67+
// console.log(B(input17_sample));
68+
console.log(B(input17));

0 commit comments

Comments
 (0)