-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17.go
111 lines (87 loc) · 2.08 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
108
109
110
111
package main
import (
"math"
"mathutil"
"os"
"panic"
"readers"
"strconv"
"strings"
)
type TargetArea struct {
xMin int
xMax int
yMin int
yMax int
}
func Day17Part1() int {
file, err := os.Open("assets/day17.txt")
panic.Check(err)
lines, err := readers.ReadStrings(file)
panic.Check(err)
targetArea := GetTargetRange(lines[0])
yMax := math.MinInt
for y := -1000; y < 1000; y++ {
for x := -1000; x < 1000; x++ {
if DidHit, yMaxForHit := TryHitTarget(x, y, targetArea); DidHit {
if yMaxForHit > yMax {
yMax = yMaxForHit
}
}
}
}
return yMax
}
func Day17Part2() int {
file, err := os.Open("assets/day17.txt")
panic.Check(err)
lines, err := readers.ReadStrings(file)
panic.Check(err)
targetArea := GetTargetRange(lines[0])
hitCount := 0
for y := -1000; y < 1000; y++ {
for x := -1000; x < 1000; x++ {
if DidHit, _ := TryHitTarget(x, y, targetArea); DidHit {
hitCount++
}
}
}
return hitCount
}
func TryHitTarget(xVelocity, yVelocity int, targetArea TargetArea) (bool, int) {
position := mathutil.Point{}
yMax := 0
for position.X <= targetArea.xMax && position.Y >= targetArea.yMin {
if position.Y > yMax {
yMax = position.Y
}
if position.X >= targetArea.xMin && position.Y <= targetArea.yMax {
return true, yMax
}
position.X += xVelocity
position.Y += yVelocity
if xVelocity > 0 {
xVelocity--
} else if xVelocity < 0 {
xVelocity++
}
yVelocity--
}
return false, yMax
}
func GetTargetRange(line string) TargetArea {
targetArea := TargetArea{}
var err error
targets := strings.Split(strings.Replace(line, "target area: ", "", 1), ", ")
xRangeStrings := strings.Split(strings.Replace(targets[0], "x=", "", 1), "..")
yRangeStrings := strings.Split(strings.Replace(targets[1], "y=", "", 1), "..")
targetArea.xMin, err = strconv.Atoi(xRangeStrings[0])
panic.Check(err)
targetArea.xMax, err = strconv.Atoi(xRangeStrings[1])
panic.Check(err)
targetArea.yMin, err = strconv.Atoi(yRangeStrings[0])
panic.Check(err)
targetArea.yMax, err = strconv.Atoi(yRangeStrings[1])
panic.Check(err)
return targetArea
}