-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
122 lines (108 loc) · 2.57 KB
/
util.go
File metadata and controls
122 lines (108 loc) · 2.57 KB
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
package gatsman
import (
"errors"
"io/ioutil"
"log"
"math"
"math/rand"
"strconv"
"strings"
)
//RandMap creates random map with specificed size and city count
func RandMap(size int, citycount int) map[int]*City {
cities := make([]City, size*size)
for x := 0; x < size; x++ {
for y := 0; y < size; y++ {
cities[(x*size)+y] = City{float64(x), float64(y)}
}
}
cityMap := make(map[int]*City)
randIndexes := rand.Perm(len(cities) - 1)
for i := 1; i <= citycount; i++ {
cityMap[i] = &cities[randIndexes[i]]
}
return cityMap
}
//LoadMap function loads the city grid text file and returns a map of city objects
func LoadMap(fileName string) map[int]*City {
cityMap := make(map[int]*City)
b, err := ioutil.ReadFile(fileName)
if err != nil {
log.Fatal(err)
}
s := strings.Split(string(b), "\n")
info := strings.Split(s[0], " ")
size, err := strconv.Atoi(strings.Split(info[0], "=")[1])
if err != nil {
log.Fatal(err)
}
cities, err := strconv.Atoi(strings.Split(info[1], "=")[1])
if err != nil {
log.Fatal(err)
}
//y should be size - y when creating cities because we start at the top
for y, line := range s[1:] {
splitLine := strings.Split(line, " ")
for x, c := range splitLine {
if c != "" {
key, err := strconv.Atoi(c)
if err != nil {
log.Print(err)
} else {
if key != 0 {
cityMap[key] = &City{float64(x), float64(size - y - 1)}
}
}
}
}
}
if cities != len(cityMap) {
log.Fatal(errors.New("error parsing city file"))
}
return cityMap
}
//Distance Method to calculate the distance between two cities
func Distance(c1 *City, c2 *City) float64 {
a := c1.X - c2.X
b := c1.Y - c2.Y
return math.Sqrt(a*a + b*b)
}
//IntArrIdxOf finds the index of an int value in an int slice
func IntArrIdxOf(arr []int, value int) int {
for p, v := range arr {
if v == value {
return p
}
}
//log.Print(errors.New("value not in slice"))
return -1
}
//IntArrCont checks if the given value is in the array or slice
func IntArrCont(arr []int, value int) bool {
for _, v := range arr {
if v == value {
return true
}
}
return false
}
//GetNonAdded returns the next int element from one of the two given arrays that is not yet in the third slice
func GetNonAdded(added []int) int {
shuffled := rand.Perm(CityCount())
for _, v := range shuffled {
if !IntArrCont(added, v+1) {
return v + 1
}
}
return -1
}
//FindString finds the index of a string in an array
func FindString(arr []string, str string) int {
for p, s := range arr {
if s == str {
return p
}
}
//log.Print(errors.New("value not in slice"))
return -1
}