-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
155 lines (132 loc) · 3.48 KB
/
main.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"encoding/csv"
"flag"
"fmt"
"math/rand"
"os"
"strconv"
"time"
)
var (
flagSeed int64
)
func init() {
flag.Int64Var(&flagSeed, "seed", time.Now().UnixNano(), "seed of random, default is value of time.Now().UnixNano()")
}
func main() {
flag.Parse()
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
type applicant struct {
company string
plan plan
next bool
}
type plan string
const (
planPlaTinum plan = "platinum"
planGold plan = "gold"
planSilver plan = "silver"
planBronze plan = "bronze"
planFree plan = "free"
)
func run() error {
cr := csv.NewReader(os.Stdin)
records, err := cr.ReadAll()
if err != nil {
return err
}
records = records[1:] // skip header
applicants := make(map[plan][]applicant, len(records))
companies := make(map[string]bool, len(records))
for _, record := range records {
// skip duplicated company
if companies[record[0]] {
fmt.Fprintln(os.Stderr, record[0], "is duplicated")
continue
}
companies[record[0]] = true
a := applicant{
company: record[0],
plan: plan(record[1]),
}
a.next, err = strconv.ParseBool(record[2])
if err != nil {
return err
}
applicants[a.plan] = append(applicants[a.plan], a)
}
rnd := rand.New(rand.NewSource(flagSeed))
fmt.Println("Seed is", flagSeed)
fmt.Println()
time.Sleep(2 * time.Second)
// Platinum "Go"ld sponsor
fmt.Println(`==== Platinum "Go"ld sponsor ====`)
rnd.Shuffle(len(applicants[planPlaTinum]), func(i, j int) {
applicants[planPlaTinum][i], applicants[planPlaTinum][j] = applicants[planPlaTinum][j], applicants[planPlaTinum][i]
})
printCompany(applicants[planPlaTinum][0].company, 1*time.Second)
printCompany(applicants[planPlaTinum][1].company, 1*time.Second)
for _, a := range applicants[planPlaTinum][2:] {
if a.next {
applicants[planGold] = append(applicants[planGold], a)
}
}
fmt.Println()
// "Go"ld sponsor
fmt.Println(`==== "Go"ld sponsor ====`)
rnd.Shuffle(len(applicants[planGold]), func(i, j int) {
applicants[planGold][i], applicants[planGold][j] = applicants[planGold][j], applicants[planGold][i]
})
printCompany(applicants[planGold][0].company, 1*time.Second)
printCompany(applicants[planGold][1].company, 1*time.Second)
for _, a := range applicants[planGold][2:] {
if a.next {
applicants[planSilver] = append(applicants[planSilver], a)
}
}
fmt.Println()
// Silver sponsor
fmt.Println("==== Silver sponsor ====")
rnd.Shuffle(len(applicants[planSilver]), func(i, j int) {
applicants[planSilver][i], applicants[planSilver][j] = applicants[planSilver][j], applicants[planSilver][i]
})
for _, a := range applicants[planSilver] {
fmt.Println(a.company)
}
fmt.Println()
// Bronze sponsor
fmt.Println("==== Bronze sponsor ====")
rnd.Shuffle(len(applicants[planBronze]), func(i, j int) {
applicants[planBronze][i], applicants[planBronze][j] = applicants[planBronze][j], applicants[planBronze][i]
})
for _, a := range applicants[planBronze] {
fmt.Println(a.company)
}
fmt.Println()
// Free sponsor
fmt.Println("==== Free sponsor ====")
rnd.Shuffle(len(applicants[planFree]), func(i, j int) {
applicants[planFree][i], applicants[planFree][j] = applicants[planFree][j], applicants[planFree][i]
})
for _, a := range applicants[planFree] {
fmt.Println(a.company)
}
return nil
}
func printCompany(name string, d time.Duration) {
var n int
for _, c := range name {
fmt.Printf("%c", c)
n++
if n < 6 {
time.Sleep(d)
}
}
fmt.Println()
time.Sleep(d)
}