-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
190 lines (167 loc) · 3.83 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"bufio"
"fmt"
"io"
"math"
"strings"
lib "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
scanner := bufio.NewScanner(input)
var grid [][]bool
nRow := -1
asteroids := make(map[lib.Position]bool)
for scanner.Scan() {
nRow++
line := strings.TrimSpace(scanner.Text())
var row []bool
for col := 0; col < len(line); col++ {
v := line[col]
if v == '#' {
row = append(row, true)
asteroids[lib.Position{nRow, col}] = true
} else {
row = append(row, false)
}
}
grid = append(grid, row)
}
_, v := getMonitoringStation(grid, asteroids)
return v
}
func getMonitoringStation(grid [][]bool, asteroids map[lib.Position]bool) (lib.Position, int) {
max := 0
var best lib.Position
for position := range asteroids {
v := countVisibleAsteroinds(grid, position, asteroids)
if v > max {
max = v
best = position
}
}
return best, max
}
func countVisibleAsteroinds(grid [][]bool, from lib.Position, asteroids map[lib.Position]bool) int {
sum := 0
for asteroid := range asteroids {
if canDetect(grid, from, asteroid) {
sum++
}
}
return sum
}
func canDetect(grid [][]bool, from, to lib.Position) bool {
if from == to {
return false
}
distanceRow := to.Row - from.Row
distanceCol := to.Col - from.Col
distance := from.Manhattan(to)
deltaRow := float64(distanceRow) / float64(distance)
deltaCol := float64(distanceCol) / float64(distance)
row := float64(from.Row) + deltaRow
col := float64(from.Col) + deltaCol
for i := 0; i < distance; i++ {
if isInt(row) && isInt(col) {
if int(math.Round(row)) == to.Row && int(math.Round(col)) == to.Col {
return true
}
if grid[int(math.Round(row))][int(math.Round(col))] {
return false
}
}
row += deltaRow
col += deltaCol
}
panic(fmt.Sprintf("%v, %v", from, to))
}
func isInt(f float64) bool {
v := f - float64(int(f))
return v < 0.000001 || v > 0.99999
}
func fs2(input io.Reader, nth int) int {
scanner := bufio.NewScanner(input)
var grid [][]bool
nRow := -1
asteroids := make(map[lib.Position]bool)
for scanner.Scan() {
nRow++
line := strings.TrimSpace(scanner.Text())
var row []bool
for col := 0; col < len(line); col++ {
v := line[col]
if v == '#' {
row = append(row, true)
asteroids[lib.Position{nRow, col}] = true
} else {
row = append(row, false)
}
}
grid = append(grid, row)
}
station, _ := getMonitoringStation(grid, asteroids)
currentAngle := 90.
for {
var options []lib.Position
for asteroid := range asteroids {
if canDetect(grid, station, asteroid) {
options = append(options, asteroid)
}
}
angles := make([]float64, 0, len(options))
for _, option := range options {
angles = append(angles, calcAngle(station, option))
}
// Which angle is the closest from currentAngle but smaller
bestAngleIndex := -1
bestDistance := 361.
for i, angle := range angles {
distance := currentAngle - angle
if distance < 0 {
continue
}
if distance < bestDistance {
bestDistance = distance
bestAngleIndex = i
}
}
if bestAngleIndex == -1 {
// We need to find the max angle
max := -1.
for i, angle := range angles {
if angle > max {
max = angle
bestAngleIndex = i
}
}
}
// Shoot i
target := options[bestAngleIndex]
currentAngle = angles[bestAngleIndex] - 0.0001
if currentAngle < 0 {
currentAngle = 359.999
}
nth--
if nth == 0 {
return target.Col*100 + target.Row
}
delete(asteroids, target)
grid[target.Row][target.Col] = false
}
}
func calcAngle(a, b lib.Position) float64 {
angleRad := math.Atan2(float64(a.Row)-float64(b.Row), float64(b.Col)-float64(a.Col))
v := angleRad * 180 / math.Pi
if v < 0 {
v = 180 - v
}
return v
}
func equals(a, b float64) bool {
tolerance := 0.001
if diff := math.Abs(a - b); diff < tolerance {
return true
}
return false
}