-
Notifications
You must be signed in to change notification settings - Fork 6
/
scrabble.go
104 lines (91 loc) · 1.92 KB
/
scrabble.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
package main
import (
"fmt"
"github.com/glendc/cgreader"
)
func GetPoints(character string) uint {
switch character {
case "e", "a", "i", "o", "n", "r", "t", "l", "s", "u":
return 1
case "d", "g":
return 2
case "b", "c", "m", "p":
return 3
case "f", "h", "v", "w", "y":
return 4
case "k":
return 5
case "j", "x":
return 8
case "q", "z":
return 10
}
cgreader.Tracef("Error, unknown Character: %s", character)
return 0
}
type WordInformation struct {
points, amount uint
}
func (w WordInformation) Increase() {
w.amount++
}
func GetWordInformation(word string) map[string]WordInformation {
info := make(map[string]WordInformation)
for i := range word {
c := string(word[i])
if _, ok := info[c]; !ok {
info[c] = WordInformation{GetPoints(c), 1}
} else {
info[c].Increase()
}
}
return info
}
func GetCharacterAmount(word string) map[string]uint {
amount := make(map[string]uint)
for i := range word {
c := string(word[i])
if _, ok := amount[c]; ok {
amount[c] = 1
} else {
amount[c]++
}
}
return amount
}
func main() {
cgreader.RunStaticPrograms(
cgreader.GetFileList("../../input/scrabble_%d.txt", 7),
cgreader.GetFileList("../../output/scrabble_%d.txt", 7),
true,
func(input <-chan string, output chan string) {
var n int
fmt.Sscanf(<-input, "%d", &n)
dictionary := make([]string, n)
for i := range dictionary {
fmt.Sscanf(<-input, "%s", &dictionary[i])
}
var w string
fmt.Sscanf(<-input, "%s", &w)
word := GetWordInformation(w)
bw, bwp := "", uint(0)
for i := range dictionary {
info, points := GetCharacterAmount(dictionary[i]), uint(0)
for c, n := range info {
if value, ok := word[c]; ok {
if n > value.amount {
n = value.amount
}
points += value.points * n
} else {
points = 0
break
}
}
if points > bwp {
bwp, bw = points, dictionary[i]
}
}
output <- bw
})
}