generated from Gophing-Around/hashcode-golang-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
116 lines (99 loc) · 2.94 KB
/
builder.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
package main
import "fmt"
type Config struct {
simuDuration int
intersections int
nStreets int
nCars int
bonusPoints int
}
type Street struct {
startIntersection int
endIntersection int
name string
timeNeeded int
score int
}
type CarsPaths struct {
nStreets int
streetNames []string
}
type Intersection struct {
id int
arrivingCars int
incomingStreets map[string]*Street
outcomingStreets map[string]*Street
}
func buildConfig(inputSet string) Config {
parts := splitSpaces(inputSet)
return Config{
simuDuration: toint(parts[0]),
intersections: toint(parts[1]),
nStreets: toint(parts[2]),
nCars: toint(parts[3]),
bonusPoints: toint(parts[4]),
}
}
func buildStreets(c Config, lines []string) ([]*Street, map[string]*Street, map[int]*Intersection, []*Intersection) {
streets := make([]*Street, c.nStreets)
streetMap := make(map[string]*Street, 0)
intersectionMap := make(map[int]*Intersection)
intersectionsList := make([]*Intersection, 0)
for i := 0; i < c.nStreets; i++ {
parts := splitSpaces(lines[i])
street := &Street{
startIntersection: toint(parts[0]),
endIntersection: toint(parts[1]),
name: parts[2],
timeNeeded: toint(parts[3]),
}
intersectionA := intersectionMap[street.startIntersection]
if intersectionA == nil {
intersectionA = &Intersection{}
}
if intersectionA.outcomingStreets == nil {
intersectionA.id = street.startIntersection
intersectionA.outcomingStreets = make(map[string]*Street)
}
intersectionA.outcomingStreets[street.name] = street
intersectionMap[street.startIntersection] = intersectionA
intersectionB := intersectionMap[street.endIntersection]
if intersectionB == nil {
intersectionB = &Intersection{}
}
if intersectionB.incomingStreets == nil {
intersectionB.id = street.endIntersection
intersectionB.incomingStreets = make(map[string]*Street)
}
intersectionB.incomingStreets[street.name] = street
intersectionMap[street.endIntersection] = intersectionB
streets[i] = street
streetMap[parts[2]] = street
}
for _, intersection := range intersectionMap {
intersectionsList = append(intersectionsList, intersection)
}
return streets, streetMap, intersectionMap, intersectionsList
}
func buildCarsPaths(c Config, lines []string) []*CarsPaths {
buildCars := make([]*CarsPaths, c.nCars)
for i := 0; i < c.nCars; i++ {
parts := splitSpaces(lines[i])
buildCars[i] = &CarsPaths{
nStreets: toint(parts[0]),
streetNames: parts[1:],
}
}
return buildCars
}
func buildOutput(outputs []output) string {
result := fmt.Sprintf("%d\n", len(outputs))
for _, out := range outputs {
result += fmt.Sprintf("%d\n", out.intersectionId)
result += fmt.Sprintf("%d\n", len(out.streetsTime))
for _, streetTime := range out.streetsTime {
result += fmt.Sprintf("%s %d\n", streetTime.name, streetTime.greenLigthDuration)
}
}
return result
}