2021 Day 17 Complete
This commit is contained in:
1
2021/day17/input
Normal file
1
2021/day17/input
Normal file
@@ -0,0 +1 @@
|
||||
target area: x=277..318, y=-92..-53
|
62
2021/day17/main.go
Normal file
62
2021/day17/main.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
h "git.bullercodeworks.com/brian/adventofcode/helpers"
|
||||
)
|
||||
|
||||
// Target Area
|
||||
var x1, y1, x2, y2 int
|
||||
|
||||
func main() {
|
||||
inp := h.StdinToString()
|
||||
fmt.Sscanf(inp, "target area: x=%d..%d, y=%d..%d", &x1, &x2, &y1, &y2)
|
||||
|
||||
var maxy int
|
||||
r := make(map[h.Coordinate]bool)
|
||||
b := 500
|
||||
|
||||
for x := b * -1; x < b; x++ {
|
||||
for y := b * -1; y < b; y++ {
|
||||
v := h.Coordinate{X: x, Y: y}
|
||||
if m, ok := testForHit(h.Coordinate{}, v); ok {
|
||||
if m > maxy {
|
||||
maxy = m
|
||||
}
|
||||
r[v] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println("Maximum Peak:", maxy)
|
||||
fmt.Println("Valid Velocities:", len(r))
|
||||
}
|
||||
|
||||
func testForHit(pos h.Coordinate, vel h.Coordinate) (int, bool) {
|
||||
var maxy int
|
||||
for {
|
||||
pos.X += vel.X
|
||||
pos.Y += vel.Y
|
||||
if pos.Y > maxy {
|
||||
maxy = pos.Y
|
||||
}
|
||||
if pos.X >= x1 && pos.X <= x2 && pos.Y >= y1 && pos.Y <= y2 {
|
||||
return maxy, true
|
||||
}
|
||||
if vel.X == 0 && vel.Y < y1 {
|
||||
return maxy, false
|
||||
}
|
||||
if vel.X > 0 {
|
||||
vel.X--
|
||||
}
|
||||
if vel.X < 0 {
|
||||
vel.X++
|
||||
}
|
||||
vel.Y--
|
||||
}
|
||||
}
|
||||
|
||||
type xy struct {
|
||||
x int
|
||||
y int
|
||||
}
|
1
2021/day17/testinput
Normal file
1
2021/day17/testinput
Normal file
@@ -0,0 +1 @@
|
||||
target area: x=20..30, y=-10..-5
|
Reference in New Issue
Block a user