adventofcode/2021/day17/main.go

63 lines
1016 B
Go

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
}