Skip to content

Commit d7f4610

Browse files
committed
Day 17
1 parent eb863d3 commit d7f4610

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

Day 17 - JS/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# [Day 17](https://adventofcode.com/2021/day/17) in [JavaScript (node)](https://nodejs.org/en/)
2+
3+
### Additional input information that must be provided in the code
4+
* Bounds of simulation (yeah it is a bruteforce)

Day 17 - JS/input.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target area: x=139..187, y=-148..-89

Day 17 - JS/solution.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
fs = require('fs');
2+
3+
const file = fs.readFileSync('./input.txt', 'utf-8');
4+
const minSimX = 0
5+
const maxSimX = 5000
6+
const minSimY = -5000
7+
const maxSimY = 5000
8+
9+
const inputInformationRegex = /^.*x=(-?\d+)\.\.(-?\d+).*y=(-?\d+)\.\.(-?\d+)\s*$/i
10+
const inputVals = file.match(inputInformationRegex)
11+
const minX = parseInt(inputVals[1])
12+
const maxX = parseInt(inputVals[2])
13+
const minY = parseInt(inputVals[3])
14+
const maxY = parseInt(inputVals[4])
15+
16+
function simulateForStart(sX, sY) {
17+
let actualMaxY = 0
18+
let wasHit = false
19+
20+
let x = 0
21+
let y = 0
22+
//console.log(sX, sY)
23+
while(!(wasHit || (sY < 0 && y < minY))) {
24+
x += sX
25+
y += sY
26+
//console.log(x, y)
27+
//console.log(sX, sY)
28+
if(y > actualMaxY) {
29+
actualMaxY = y
30+
}
31+
sY -= 1
32+
sX -= Math.sign(sX)
33+
//console.log(x, minX, maxX, y, minY, maxY)
34+
if(x >= minX && x <= maxX && y >= minY && y <= maxY) {
35+
//console.log(x, y)
36+
wasHit = true
37+
}
38+
}
39+
//console.log(wasHit)
40+
return {maxY: actualMaxY, hit: wasHit}
41+
}
42+
43+
let foundYs = []
44+
45+
for(let sY = minSimY; sY <= maxSimY; sY++) {
46+
for(let sX = minSimX; sX <= maxSimX; sX++) {
47+
const res = simulateForStart(sX, sY)
48+
if(res.hit) {
49+
foundYs.push(res.maxY)
50+
}
51+
}
52+
}
53+
54+
console.log("Part 1:", Math.max(...foundYs))
55+
console.log("Part 2:", foundYs.length)

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
Done in variety of languages
33

44
### Progress
5-
![](https://img.shields.io/badge/days%20completed%20📅-16-blue)
5+
![](https://img.shields.io/badge/days%20completed%20📅-17-blue)
66

7-
![](https://img.shields.io/badge/stars%20⭐-32-yellow)
7+
![](https://img.shields.io/badge/stars%20⭐-34-yellow)
88

9-
![](https://img.shields.io/badge/languages%20💬-14-red)
9+
![](https://img.shields.io/badge/languages%20💬-15-red)
1010

1111
### Notes
1212
* In all programs input file must be provided in the source code (usually somewhere at the top)
@@ -19,7 +19,7 @@ Done in variety of languages
1919
|1|Python||14|Go|
2020
|2|C# (.NET Core 6.0)||15|Scala|
2121
|3|C++||16|Scala|
22-
|4|TypeScript||17||
22+
|4|TypeScript||17|JavaScript|
2323
|5|Kotlin||18||
2424
|6|Haskell||19||
2525
|7|R||20||

0 commit comments

Comments
 (0)