-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17.go
107 lines (94 loc) · 2.21 KB
/
day17.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/dergeberl/aoc/utils"
)
type area struct {
x1, x2 int
y1, y2 int
}
func main() {
input, err := os.ReadFile("input.txt")
if err != nil {
os.Exit(1)
}
fmt.Printf("Part 1: %v\n", SolveDay17Part1(string(input)))
fmt.Printf("Part 2: %v\n", SolveDay17Part2(string(input)))
}
//SolveDay17Part1 returns the highest reachable y value for a target area
func SolveDay17Part1(input string) int {
a := parseInput(input)
return utils.GetGaussscheSummenformel((a.y1 * -1) - 1)
}
//SolveDay17Part2 returns the number of possible shots for a target area
func SolveDay17Part2(input string) int {
a := parseInput(input)
var hits int
for x := 0; x <= a.x2; x++ {
for y := a.y1; y <= (a.y1 * -1); y++ {
if a.checkShot(x, y) {
hits++
}
}
}
return hits
}
//checkShot returns true shot is possible and returns the highest reached y
func (a area) checkShot(x, y int) bool {
curX, curY := 0, 0
for a.isReachableByPoint(curX, curY) {
curX += x
curY += y
if a.checkPoint(curX, curY) {
return true
}
if x > 0 {
x--
}
y--
}
return false
}
//checkPoint returns true if point is in area
func (a area) checkPoint(x, y int) bool {
return y >= a.y1 && y <= a.y2 && x >= a.x1 && x <= a.x2
}
//isReachableByPoint returns if the area is reachable by a point
func (a area) isReachableByPoint(x, y int) bool {
return !(y < a.y1 || x > a.x2)
}
//parseInput returns the given target area
func parseInput(input string) area {
input = strings.TrimSuffix(input, "\n")
input = strings.TrimPrefix(input, "target area: ")
var a area
for _, v := range strings.Split(input, ",") {
v = strings.TrimSpace(v)
if strings.HasPrefix(v, "x") {
xVals := strings.Split(strings.TrimPrefix(v, "x="), "..")
if len(xVals) != 2 {
panic("wrong input")
}
a.x1, _ = strconv.Atoi(xVals[0])
a.x2, _ = strconv.Atoi(xVals[1])
}
if strings.HasPrefix(v, "y") {
yVals := strings.Split(strings.TrimPrefix(v, "y="), "..")
if len(yVals) != 2 {
panic("wrong input")
}
a.y1, _ = strconv.Atoi(yVals[0])
a.y2, _ = strconv.Atoi(yVals[1])
}
}
if a.y1 > a.y2 {
a.y1, a.y2 = a.y2, a.y1
}
if a.x1 > a.x2 {
a.x1, a.x2 = a.x2, a.x1
}
return a
}