-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathone_eyed_robot.go
72 lines (60 loc) · 917 Bytes
/
one_eyed_robot.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"
import "math/rand"
import "time"
var numBuckets = 20
type Buckets []int
func randBall() int {
return rand.Intn(3)
}
func RandBuckets() Buckets {
b := make(Buckets, numBuckets)
for i := 0; i < numBuckets; i++ {
b[i] = randBall()
}
return b
}
func (b Buckets) Show() {
s := " "
for _, bucket := range b {
switch bucket {
case 0:
s += "RED "
case 1:
s += "GREEN "
case 2:
s += "BLUE "
}
}
fmt.Println(s)
}
func (b *Buckets) Swap(m,n int) {
(*b)[n], (*b)[m] = (*b)[m], (*b)[n]
}
func (b Buckets) RunRobot() {
b.Show()
lastRed := 0
firstBlue := (numBuckets-1)
i := 0
for i < firstBlue {
switch b[i] {
case 0:
b.Swap(i, lastRed)
lastRed++
i++
case 1:
i++
case 2:
b.Swap(i, firstBlue)
firstBlue--
}
}
b.Show()
}
func init() {
rand.Seed( time.Now().UTC().UnixNano())
}
func main() {
b := RandBuckets()
b.RunRobot()
}