-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (65 loc) · 1.49 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"fmt"
"math"
"os"
"strconv"
"github.com/jyggen/advent-of-go/internal/solver"
"github.com/jyggen/advent-of-go/internal/utils"
)
func main() {
p1, p2, err := solver.SolveFromFile(os.Stdin, SolvePart1, SolvePart2)
if err != nil {
panic(err)
}
fmt.Println(p1)
fmt.Println(p2)
}
func SolvePart1(input string) (string, error) {
best, _ := simulate(input)
return strconv.Itoa(best), nil
}
func SolvePart2(input string) (string, error) {
_, result := simulate(input)
return strconv.Itoa(result), nil
}
func simulate(input string) (int, int) {
parts := utils.ToStringSlice(input, ", ")
xParts := utils.ToStringSlice(parts[0][15:], "..")
yParts := utils.ToStringSlice(parts[1][2:], "..")
x1, _ := strconv.Atoi(xParts[0])
x2, _ := strconv.Atoi(xParts[1])
y1, _ := strconv.Atoi(yParts[0])
y2, _ := strconv.Atoi(yParts[1])
validInits := 0
bestY := 0
for xInit := int(math.Sqrt(float64(x1 * 2))); xInit <= x2; xInit++ {
for yInit := y1; yInit < -y1+1; yInit++ {
probe := [2]int{0, 0}
xVel := xInit
yVel := yInit
highest := 0
for probe[0] < x2 && probe[1] > y1 {
probe[0] += xVel
probe[1] += yVel
if xVel > 0 {
xVel--
} else if xVel < 0 {
xVel++
}
if probe[1] > highest {
highest = probe[1]
}
yVel--
if probe[0] >= x1 && probe[0] <= x2 && probe[1] >= y1 && probe[1] <= y2 {
validInits++
if highest > bestY {
bestY = highest
}
break
}
}
}
}
return bestY, validInits
}