generated from Gophing-Around/hashcode-golang-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
builder.go
131 lines (110 loc) · 2.75 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import "fmt"
type Config struct {
contributors int
projects int
}
type Contributor struct {
name string
nSkills int
allocated bool
skills map[string]int
}
type Project struct {
name string
nDays int
score int
bestBefore int
nRoles int
rolesList []*Role
rolesMap map[string]*Role
alreadyPlanned bool
}
type Role struct {
id int
name string
level int
mentored bool
}
func buildInput(inputSet string) (int, *Config, []*Contributor, []*Project, map[string][]*Contributor) {
lines := splitNewLines(inputSet)
configLine := splitSpaces(lines[0])
fmt.Printf("Config line: %v\n", configLine)
config := &Config{
contributors: toint(configLine[0]),
projects: toint(configLine[1]),
}
contributors := make([]*Contributor, 0)
projects := make([]*Project, 0)
rolesContributor := make(map[string][]*Contributor)
i := 1
maxDays := 0
k := 0
for ; k < config.contributors; k++ {
contribLine := splitSpaces(lines[i])
contrib := &Contributor{
name: contribLine[0],
nSkills: toint(contribLine[1]),
}
i++
skill := make(map[string]int)
j := 0
for ; j < contrib.nSkills; j++ {
skillLine := splitSpaces(lines[i+j])
skillName := skillLine[0]
skill[skillName] = toint(skillLine[1])
list, ok := rolesContributor[skillName]
if !ok {
list = make([]*Contributor, 0)
}
list = append(list, contrib)
rolesContributor[skillName] = list
}
i += j
contrib.skills = skill
contributors = append(contributors, contrib)
}
for k := 0; k < config.projects; k++ {
projectLine := splitSpaces(lines[i])
i++
project := Project{
name: projectLine[0],
nDays: toint(projectLine[1]),
score: toint(projectLine[2]),
bestBefore: toint(projectLine[3]),
nRoles: toint(projectLine[4]),
}
if maxDays < project.bestBefore+project.score-project.nDays {
maxDays = project.bestBefore + project.score - project.nDays
}
j := 0
roles := make(map[string]*Role)
rolesList := make([]*Role, 0)
for ; j < project.nRoles; j++ {
roleLine := splitSpaces(lines[i+j])
role := Role{
id: j,
name: roleLine[0],
level: toint(roleLine[1]),
}
roles[roleLine[0]] = &role
rolesList = append(rolesList, &role)
}
i += j
project.rolesMap = roles
project.rolesList = rolesList
projects = append(projects, &project)
}
return maxDays, config, contributors, projects, rolesContributor
}
func buildOutput(plannedProjects []*PlannedProject) string {
result := fmt.Sprintf("%d\n", len(plannedProjects))
for _, project := range plannedProjects {
result += fmt.Sprintf("%s\n", project.name)
for _, contrib := range project.contributors {
result += fmt.Sprintf("%s ", contrib.name)
}
result += "\n"
}
return result
}