-
Notifications
You must be signed in to change notification settings - Fork 0
/
jugglefest.go
92 lines (78 loc) · 1.79 KB
/
jugglefest.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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"github.com/bcgraham/jugglefest/solution"
"os"
"strings"
//"sort"
)
var input = flag.String("input", "", "path to the input file")
var output = flag.String("output", "", "path to the output file")
func main() {
flag.Parse()
cs, js, err := readLines(*input)
if err != nil {
panic(err)
}
r := make(solution.Jugglers, 0)
capacity := len(js) / len(cs)
s := &solution.Solution{cs, js, r, capacity}
s.AssignJugglers()
// Run through rejected jugglers, giving them preference in ties.
s.Unassigned = s.Rejected
s.Rejected = make(solution.Jugglers, 0)
s.AssignJugglers()
fmt.Printf("%v unmatched jugglers.", len(s.Rejected))
// Assign remaining jugglers at random.
for _, c := range s.Circuits {
for len(c.Accepted) < 6 {
c.Accepted = append(c.Accepted, s.Rejected.Pop())
}
}
results := s.Circuits.Publish()
//sort.Strings(results)
writeFile, err := os.Create(*output)
if err != nil {
panic(err)
}
defer writeFile.Close()
w := bufio.NewWriter(writeFile)
var line bytes.Buffer
for _, r := range results {
line.Reset()
line.WriteString(r)
fmt.Fprintln(w, line.String())
}
w.Flush()
}
func readLines(path string) (cs solution.Circuits, js solution.Jugglers, err error) {
file, err := os.Open(path)
if err != nil {
return nil, nil, err
}
defer file.Close()
cs = make(solution.Circuits)
js = make(solution.Jugglers, 0)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "C") {
c, err := solution.MakeCircuit(line)
if err != nil {
panic(err)
}
cs[c.Name] = c
}
if strings.HasPrefix(line, "J") {
j, err := solution.MakeJuggler(line, cs)
if err != nil {
panic(err)
}
js = append(js, j)
}
}
return cs, js, scanner.Err()
}