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)

	// Let's just see if -500 to 500 is enough >.<
	for x := 500 * -1; x < 500; x++ {
		for y := 500 * -1; y < 500; 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
}
