mirror of
https://github.com/seigler/aoc2021
synced 2025-07-26 17:06:09 +00:00
day 17
This commit is contained in:
parent
fff1d02a23
commit
9f368e7d9c
4 changed files with 83 additions and 13 deletions
|
@ -241,16 +241,18 @@
|
|||
},
|
||||
{
|
||||
"part1": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"solved": true,
|
||||
"result": "4278",
|
||||
"attempts": [
|
||||
"4380"
|
||||
],
|
||||
"time": 218.59
|
||||
},
|
||||
"part2": {
|
||||
"solved": false,
|
||||
"result": null,
|
||||
"solved": true,
|
||||
"result": "1994",
|
||||
"attempts": [],
|
||||
"time": null
|
||||
"time": 211.79
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
12
README.md
12
README.md
|
@ -27,7 +27,7 @@
|
|||
[](src/day14)
|
||||
[](src/day15)
|
||||
[](src/day16)
|
||||

|
||||
[](src/day17)
|
||||

|
||||

|
||||

|
||||
|
@ -181,9 +181,9 @@ Both parts: 1.4ms
|
|||
|
||||
```
|
||||
Day 17
|
||||
Time part 1: -
|
||||
Time part 2: -
|
||||
Both parts: -
|
||||
Time part 1: 218.59ms
|
||||
Time part 2: 211.79ms
|
||||
Both parts: 430.38ms
|
||||
```
|
||||
|
||||
```
|
||||
|
@ -243,8 +243,8 @@ Both parts: -
|
|||
```
|
||||
|
||||
```
|
||||
Total stars: 32/50
|
||||
Total time: 1902.9999999999998ms
|
||||
Total stars: 34/50
|
||||
Total time: 2333.3799999999997ms
|
||||
```
|
||||
|
||||
<!--/RESULTS-->
|
||||
|
|
9
src/day17/README.md
Normal file
9
src/day17/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# 🎄 Advent of Code 2021 - day 17 🎄
|
||||
|
||||
## Info
|
||||
|
||||
Task description: [link](https://adventofcode.com/2021/day/17)
|
||||
|
||||
## Notes
|
||||
|
||||
...
|
59
src/day17/index.js
Normal file
59
src/day17/index.js
Normal file
|
@ -0,0 +1,59 @@
|
|||
import run from "aocrunner"
|
||||
|
||||
const parseInput = (rawInput) => {
|
||||
const [, , a, b, , c, d] = rawInput.trim().split(/[:,] |\.\.|=/)
|
||||
return [+a, +b, +c, +d]
|
||||
}
|
||||
|
||||
const part1 = (rawInput, part1 = true) => {
|
||||
const [minX, maxX, minY, maxY] = parseInput(rawInput)
|
||||
|
||||
function fire(startX, startY) {
|
||||
let dX = startX,
|
||||
dY = startY,
|
||||
x = 0,
|
||||
y = 0,
|
||||
highest = 0
|
||||
while (x <= maxX && y >= minY) {
|
||||
x += dX
|
||||
y += dY
|
||||
dX -= Math.sign(dX)
|
||||
dY -= 1
|
||||
if (y > highest) {
|
||||
highest = y
|
||||
}
|
||||
if (x >= minX && x <= maxX && y >= minY && y <= maxY) {
|
||||
return highest
|
||||
}
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
let bestY = 0
|
||||
let count = 0
|
||||
for (let dy = minY; dy < 1000; dy++) {
|
||||
for (let dx = 1; dx <= maxX; dx++) {
|
||||
const newY = fire(dx, dy)
|
||||
if (newY !== undefined) count++
|
||||
if (newY > bestY) bestY = newY
|
||||
}
|
||||
}
|
||||
if (part1) return bestY
|
||||
return count
|
||||
}
|
||||
|
||||
const part2 = (rawInput) => {
|
||||
return part1(rawInput, false)
|
||||
}
|
||||
|
||||
run({
|
||||
part1: {
|
||||
tests: [{ input: `target area: x=20..30, y=-10..-5`, expected: 45 }],
|
||||
solution: part1,
|
||||
},
|
||||
part2: {
|
||||
tests: [{ input: `target area: x=20..30, y=-10..-5`, expected: 112 }],
|
||||
solution: part2,
|
||||
},
|
||||
trimTestInputs: true,
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue