generated from Gophing-Around/hashcode-golang-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
80 lines (67 loc) · 1.55 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
package main
import (
"fmt"
"sort"
"strings"
)
type Config struct {
rows int
slots int
unavailable int
nPools int
nServers int
}
type Server struct {
capacity int
size int
id int
assignedRow int
assignedSlot int
assignedPool int
assigned bool
}
type unavailablesMap map[string]bool
func buildInput(inputSet string) (Config, unavailablesMap, []*Server) {
lines := splitNewLines(inputSet)
configItem := splitSpaces(lines[0])
config := Config{
rows: toint(configItem[0]),
slots: toint(configItem[1]),
unavailable: toint(configItem[2]),
nPools: toint(configItem[3]),
nServers: toint(configItem[4]),
}
unavailbles := make(unavailablesMap, 0)
for i := 1; i <= config.unavailable; i++ {
unavailbles[lines[i]] = true
}
servers := make([]*Server, 0)
for i := 1 + config.unavailable; i <= config.unavailable+config.nServers; i++ {
line := splitSpaces(lines[i])
servers = append(servers, &Server{
id: i - 1 - config.unavailable,
size: toint(line[0]),
capacity: toint(line[1]),
})
}
return config, unavailbles, servers
}
func buildOutput(servers []*Server) string {
sort.Slice(servers, func(i, j int) bool {
a := servers[i]
b := servers[j]
if a.id < b.id {
return true
}
return false
})
result := ""
for _, server := range servers {
if server.assigned {
result += fmt.Sprintf("%d %d %d\n", server.assignedRow, server.assignedSlot, server.assignedPool)
} else {
result += fmt.Sprintf("x\n")
}
}
return strings.TrimRight(result, "\n")
}