Added solutions for 17th day: target practice
This commit is contained in:
parent
f169c1bbd9
commit
3f49055c82
4 changed files with 125 additions and 0 deletions
1
2021/17/example
Normal file
1
2021/17/example
Normal file
|
@ -0,0 +1 @@
|
||||||
|
target area: x=20..30, y=-10..-5
|
69
2021/17/first.go
Normal file
69
2021/17/first.go
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type vector struct {
|
||||||
|
x, y int
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
scanner.Scan()
|
||||||
|
line := scanner.Text()
|
||||||
|
elts := strings.Split(line, "..")
|
||||||
|
eltsx1 := strings.Split(elts[0], "=")
|
||||||
|
x1, _ := strconv.Atoi(eltsx1[1])
|
||||||
|
eltsx2 := strings.Split(elts[1], ",")
|
||||||
|
x2, _ := strconv.Atoi(eltsx2[0])
|
||||||
|
eltsy1 := strings.Split(elts[1], "=")
|
||||||
|
y1, _ := strconv.Atoi(eltsy1[1])
|
||||||
|
y2, _ := strconv.Atoi(elts[2])
|
||||||
|
|
||||||
|
// We need to calculate which steps and horizontal speeds can hit the target box
|
||||||
|
x := 0
|
||||||
|
vx := 0 // its a vx max more than a vx
|
||||||
|
i := 1
|
||||||
|
for {
|
||||||
|
if x+vx+1 <= x2 {
|
||||||
|
vx += 1
|
||||||
|
x += vx
|
||||||
|
i++
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
hmax := 0
|
||||||
|
for nextVy := 0; nextVy < 10000; nextVy++ {
|
||||||
|
y := 0
|
||||||
|
vy := nextVy
|
||||||
|
tmpHmax := 0
|
||||||
|
j := 0
|
||||||
|
for {
|
||||||
|
if y+vy >= y1 {
|
||||||
|
y += vy
|
||||||
|
vy--
|
||||||
|
j++
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if tmpHmax < y {
|
||||||
|
tmpHmax = y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if y >= y1 && y <= y2 && hmax < tmpHmax {
|
||||||
|
hmax = tmpHmax
|
||||||
|
fmt.Printf("h=%d, y=%d, vy=%d, j=%d\n", hmax, y, nextVy, j)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("x1=%d, x2=%d, y1=%d, y2=%d\n", x1, x2, y1, y2)
|
||||||
|
fmt.Printf("i=%d, x=%d, vx=%d\n", i, x, vx)
|
||||||
|
fmt.Println(hmax)
|
||||||
|
}
|
1
2021/17/input
Normal file
1
2021/17/input
Normal file
|
@ -0,0 +1 @@
|
||||||
|
target area: x=265..287, y=-103..-58
|
54
2021/17/second.go
Normal file
54
2021/17/second.go
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type vector struct {
|
||||||
|
x, y int
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
scanner.Scan()
|
||||||
|
line := scanner.Text()
|
||||||
|
elts := strings.Split(line, "..")
|
||||||
|
eltsx1 := strings.Split(elts[0], "=")
|
||||||
|
x1, _ := strconv.Atoi(eltsx1[1])
|
||||||
|
eltsx2 := strings.Split(elts[1], ",")
|
||||||
|
x2, _ := strconv.Atoi(eltsx2[0])
|
||||||
|
eltsy1 := strings.Split(elts[1], "=")
|
||||||
|
y1, _ := strconv.Atoi(eltsy1[1])
|
||||||
|
y2, _ := strconv.Atoi(elts[2])
|
||||||
|
|
||||||
|
possible := 0
|
||||||
|
for nextVx := 0; nextVx <= x2; nextVx++ {
|
||||||
|
out:
|
||||||
|
for nextVy := y1; nextVy < 1000; nextVy++ {
|
||||||
|
vx := nextVx
|
||||||
|
vy := nextVy
|
||||||
|
x := 0
|
||||||
|
y := 0
|
||||||
|
for {
|
||||||
|
if x >= x1 && x <= x2 && y >= y1 && y <= y2 {
|
||||||
|
possible++
|
||||||
|
continue out
|
||||||
|
}
|
||||||
|
if x > x2 || y < y1 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if vx >= 0 {
|
||||||
|
x += vx
|
||||||
|
vx--
|
||||||
|
}
|
||||||
|
y += vy
|
||||||
|
vy--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println(possible)
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue