-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
104 lines (87 loc) · 1.63 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
aoc "github.com/teivah/advent-of-code"
)
func fs1(from, to position) int {
b := Board{from, to}
return b.best()
}
func (b Board) best() int {
const (
maxRow = 100
maxCol = 100
)
best := aoc.NewMaxer()
for row := 0; row <= maxRow; row++ {
for col := 0; col <= maxCol; col++ {
h, inside := b.fire(position{row, col})
if inside {
best.Add(h)
}
}
}
return best.Get()
}
func (b Board) fire(velocity position) (int, bool) {
var current position
var previous position
maxHeight := aoc.NewMaxer()
for {
current.col += velocity.col
current.row += velocity.row
velocity.row--
if velocity.col > 0 {
velocity.col--
} else if velocity.col < 0 {
velocity.col++
}
maxHeight.Add(current.row)
if b.isInsideTarget(current) {
return maxHeight.Get(), true
}
if b.isOver(current, current.row < previous.row) {
return 0, false
}
previous = current
}
}
type Board struct {
from position
to position
}
func (b Board) isInsideTarget(pos position) bool {
return pos.row >= b.from.row && pos.row <= b.to.row &&
pos.col >= b.from.col && pos.col <= b.to.col
}
func (b Board) isOver(pos position, down bool) bool {
if !down {
return false
}
return pos.row < b.from.row
}
type position struct {
row int
col int
}
func fs2(from, to position) int {
b := Board{from, to}
return b.all()
}
func (b Board) all() int {
const (
minRow = -100
minCol = -100
maxRow = 400
maxCol = 400
)
sum := 0
for row := minRow; row <= maxRow; row++ {
for col := minCol; col <= maxCol; col++ {
_, inside := b.fire(position{row, col})
if inside {
sum++
}
}
}
return sum
}