generated from Gophing-Around/hashcode-golang-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm.go
135 lines (111 loc) · 2.8 KB
/
algorithm.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
package main
type StreetTime struct {
name string
greenLigthDuration int
}
type output struct {
intersectionId int
intersection Intersection
streetsTime []StreetTime
}
func algorithm(
config Config,
streets []*Street,
carsPaths []*CarsPaths,
streetsMap map[string]*Street,
intersectionMap map[int]*Intersection,
intersectionsList []*Intersection,
) []output {
out := make([]output, 0)
for _, carPath := range carsPaths {
var intersectionId int
var totTime int
for _, street := range carPath.streetNames {
street := streetsMap[street]
totTime += street.timeNeeded
intersectionId = street.endIntersection
}
if totTime <= config.simuDuration {
intersection := intersectionMap[intersectionId]
intersection.arrivingCars++
intersectionMap[intersectionId] = intersection
}
}
for _, intersection := range intersectionsList {
visited := make(map[int]bool)
dfs(
visited,
config.simuDuration,
intersection,
intersection.arrivingCars,
intersection,
intersectionMap,
)
}
for _, intersection := range intersectionsList {
streetTimes := make([]StreetTime, 0)
totScore := 0
for _, street := range intersection.incomingStreets {
totScore += int(street.score)
}
if totScore == 0 {
continue
}
a := len(intersection.incomingStreets) / len(intersection.outcomingStreets)
if a == 0 {
a = 1
}
for _, street := range intersection.incomingStreets {
if street.score == 0 {
continue
}
streetTimes = append(streetTimes, StreetTime{
name: street.name,
greenLigthDuration: a, // / len(intersection.incomingStreets),
})
}
out = append(out, output{
intersectionId: intersection.id,
streetsTime: streetTimes,
})
}
return out
}
func dfs(
visited map[int]bool,
remainingTime int,
// incomingStreet *Street,
intersection *Intersection,
score int,
startIntersection *Intersection,
intersectionMap map[int]*Intersection,
) int {
if visited := visited[intersection.id]; visited {
return score
}
visited[intersection.id] = true
// score += intersection.arrivingCars // TODO
for streetName, incomingStreet := range intersection.incomingStreets {
streetScore := dfs(
visited,
remainingTime-incomingStreet.timeNeeded,
// incomingStreet,
intersectionMap[incomingStreet.startIntersection],
score+int(intersectionMap[incomingStreet.startIntersection].arrivingCars),
startIntersection,
intersectionMap,
)
// score += streetScore
incomingStreet.score = streetScore
intersection.incomingStreets[streetName] = incomingStreet
}
// visited[intersection.id] = false
return score
}
func pickBestIntersection(intersectionsList []*Intersection) *Intersection {
return intersectionsList[0]
}
type IntersectionNode struct {
data *Intersection
nextIntersections []*Intersection
}