-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd10.go
147 lines (120 loc) · 2.38 KB
/
d10.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
package main
import (
"fmt"
"io/ioutil"
"math"
"os"
"sort"
"strings"
)
type Pair struct{ x, y int }
type FPair struct{ x, y float64 }
type Pairs []Pair
type VMap map[int][]int
func (m VMap) put(at int, v int) {
arr, ok := m[at]
if !ok {
arr = make([]int, 0)
}
arr = append(arr, v)
m[at] = arr
}
func unit(a, b Pair) FPair {
d := dist(a, b)
u := FPair{float64(a.y-b.y) / d, float64(a.x-b.x) / d}
return u
}
func dist(a, b Pair) float64 {
return math.Sqrt(math.Pow(float64(a.x-b.x), 2) + math.Pow(float64(a.y-b.y), 2))
}
func fdist(a, b FPair) float64 {
return math.Sqrt(math.Pow(a.x-b.x, 2) + math.Pow(a.y-b.y, 2))
}
func rot(a, b FPair) float64 {
v := math.Atan2(a.x*b.y-a.y*b.x, a.x*b.x+a.y*b.y)
if v < 0 {
v += 2 * math.Pi
}
return v / math.Pi * 180
}
func main() {
raw, _ := ioutil.ReadFile(os.Args[1])
lines := strings.Split(strings.Trim(string(raw), "\n "), "\n")
mets := make([]Pair, 0)
for j, line := range lines {
for i, c := range line {
if c == '#' {
mets = append(mets, Pair{i, j})
}
}
}
vm := make(VMap)
for i := 0; i < len(mets)-1; i++ {
for j := i + 1; j < len(mets); j++ {
inSight := true
a, b := mets[i], mets[j]
u := unit(a, b)
d := dist(a, b)
for k := 0; k < len(mets); k++ {
if k == i || k == j {
continue
}
c := mets[k]
uc := unit(a, c)
if fdist(u, uc) < 0.0001 && dist(a, c) < d {
inSight = false
break
}
}
if inSight {
vm.put(i, j)
vm.put(j, i)
}
}
}
maxv := 0
maxi := 0
for i, v := range vm {
if len(v) > maxv {
maxv = len(v)
maxi = i
}
}
fmt.Println("Result1:", maxv)
type Rec struct {
u FPair
r float64
n float64
d float64
i int
}
type Records []Rec
up := FPair{1, 0}
recs := make(Records, 0, len(mets)-1)
mstation := mets[maxi]
for i := 0; i < len(mets); i++ {
if i == maxi {
continue
}
b := mets[i]
ro := rot(unit(mstation, b), up)
recs = append(recs, Rec{unit(mstation, b), ro, 0, dist(mstation, b), i})
}
Less := func(i, j int) bool {
a, b := recs[i], recs[j]
ar, br := a.r+360*a.n, b.r+360*b.n
return ar < br || (ar == br && a.d < b.d)
}
sort.Slice(recs, Less)
cnt := 1
for i := 1; i < len(recs); i++ {
if recs[i-1].r == recs[i].r {
recs[i].n = float64(cnt)
cnt += 1
} else {
cnt = 1
}
}
sort.Slice(recs, Less)
fmt.Println("Result2:", mets[recs[199].i].x*100+mets[recs[199].i].y)
}