-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgogreat.go
72 lines (67 loc) · 1.44 KB
/
gogreat.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
package main
import(
"fmt"
"runtime"
"os"
"strconv"
)
func sendSidewards(from chan int, to chan int){
for{
n := <-from
to <-n
}
}
func makeTrack(left chan int, numLeft int){
if numLeft > 0{
right := make(chan int)
go makeTrack(right, numLeft-1)
go sendSidewards(left, right)
sendSidewards(right, left)
}else{
for{
n := <-left
left<- n
}
}
}
func doRace(start chan int, resultChan chan []int, numRunners int){
results := make([]int,0,numRunners)
for i := 0; i < numRunners; i++{
start <- i
}
for i := 0; i < numRunners; i++{
n := <-start
results = append(results, n)
}
resultChan <- results
}
func main(){
if len(os.Args) != 3{
panic("Error: program takes two arguments, runners and threads.")
}
inThreads := os.Args[2]
inRunners := os.Args[1]
runtime.GOMAXPROCS(4)
numThreads, err := strconv.Atoi(inThreads)
if err != nil{
panic("Error: threads must be a int.")
}
numRunners, err := strconv.Atoi(inRunners)
if err != nil{
panic("Error: runners must be a int.")
}
racerA := make(chan int)
racerB := make(chan int)
go makeTrack(racerA, numThreads)
go makeTrack(racerB, numThreads)
resultsA := make(chan []int)
resultsB := make(chan []int)
go doRace(racerA, resultsA, numRunners)
go doRace(racerB, resultsB, numRunners)
select{
case results := <- resultsA:
fmt.Printf("A won! With results: \n %v\n", results)
case results := <- resultsB:
fmt.Printf("B won! With results: \n %v\n", results)
}
}