-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17.go
78 lines (69 loc) · 1.03 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
package main
import (
"fmt"
)
type target struct {
xmin int
xmax int
ymin int
ymax int
}
func main() {
//target area: x=185..221, y=-122..-74
t := target{185, 221, -122, -74}
//t = target{20, 30, -10, -5}
// Part1
res := 0
for x := 0; x < t.xmax; x++ {
if (x*x+1)/2 < t.xmin {
continue
}
for y := 0; y < 5000; y++ {
hit, my := hitsTarget(x, y, t)
if hit {
if my > res {
res = my
}
}
}
}
fmt.Println(res)
// Part2
res = 0
type c struct {
x int
y int
}
vels := make(map[c]bool)
for x := 0; x <= t.xmax; x++ {
for y := -5000; y < 5000; y++ {
hit, _ := hitsTarget(x, y, t)
if hit {
vels[c{x, y}] = true
}
}
}
fmt.Println(len(vels))
}
func hitsTarget(dx int, dy int, t target) (bool, int) {
x, y := 0, 0
maxY := 0
for true {
x = x + dx
y = y + dy
if y > maxY {
maxY = y
}
if x >= t.xmin && x <= t.xmax && y >= t.ymin && y <= t.ymax {
return true, maxY
}
if dx > 0 {
dx--
}
dy--
if x > t.xmax || y < t.ymin {
break
}
}
return false, maxY
}