-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonkey.go
86 lines (71 loc) · 2.06 KB
/
monkey.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
package main
import (
"fmt"
"math/rand"
"time"
)
const letterBytes = "abcdefghijklmnopqrstuvwxyz"
func randString(length int) string {
result := make([]byte, length)
for i := range result {
result[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(result)
}
func printStats(monkey int, guesses int, totalTime time.Duration) {
fmt.Printf("Monkey #%d: In %d guesses.\n", monkey, guesses)
fmt.Printf("Total time: %.2f sseconds\n", totalTime.Seconds())
fmt.Printf("%dns per guess.\n\n", totalTime.Nanoseconds() / int64(guesses))
}
func monkey(words <-chan string, result chan<- int) {
current_word := ""
guesses := 0
for true {
select {
case word := <- words:
guesses = 0
current_word = word
default:
if current_word != "" {
guess := randString(len(current_word))
guesses++
if guess == current_word {
result <- guesses
current_word = ""
}
}
}
}
}
func main() {
rand.Seed(time.Now().UnixNano())
words_channels := make([]chan string, 4)
result_channels := make([]chan int, 4)
for j := 0; j < 4; j++ {
words_channels[j] = make(chan string)
result_channels[j] = make(chan int)
go monkey(words_channels[j], result_channels[j])
}
for i := 4; i <= 8; i++ {
word := randString(i)
fmt.Printf("Word to guess: %s\n", word)
startTime := time.Now()
for _, word_channel := range words_channels {
word_channel <- word
}
select {
case guesses := <- result_channels[0]:
totalTime := time.Since(startTime)
printStats(0, guesses, totalTime)
case guesses := <- result_channels[1]:
totalTime := time.Since(startTime)
printStats(1, guesses, totalTime)
case guesses := <- result_channels[2]:
totalTime := time.Since(startTime)
printStats(2, guesses, totalTime)
case guesses := <- result_channels[3]:
totalTime := time.Since(startTime)
printStats(3, guesses, totalTime)
}
}
}