| 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 |
package main |
| 40 |
|
| 41 |
import ( |
| 42 |
"flag" |
| 43 |
"fmt" |
| 44 |
"strconv" |
| 45 |
) |
| 46 |
|
| 47 |
const ( |
| 48 |
blue = iota |
| 49 |
red |
| 50 |
yellow |
| 51 |
ncol |
| 52 |
) |
| 53 |
|
| 54 |
var complement = [...]int{ |
| 55 |
red | red<<2: red, |
| 56 |
red | yellow<<2: blue, |
| 57 |
red | blue<<2: yellow, |
| 58 |
yellow | red<<2: blue, |
| 59 |
yellow | yellow<<2: yellow, |
| 60 |
yellow | blue<<2: red, |
| 61 |
blue | red<<2: yellow, |
| 62 |
blue | yellow<<2: red, |
| 63 |
blue | blue<<2: blue, |
| 64 |
} |
| 65 |
|
| 66 |
var colname = [...]string{ |
| 67 |
blue: "blue", |
| 68 |
red: "red", |
| 69 |
yellow: "yellow", |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
type info struct { |
| 74 |
colour int |
| 75 |
name int |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
type rendez struct { |
| 83 |
n int |
| 84 |
mate chan<- info |
| 85 |
info info |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
type result struct { |
| 90 |
met int |
| 91 |
same int |
| 92 |
} |
| 93 |
|
| 94 |
var n = 600 |
| 95 |
|
| 96 |
func main() { |
| 97 |
flag.Parse() |
| 98 |
if flag.NArg() > 0 { |
| 99 |
n, _ = strconv.Atoi(flag.Arg(0)) |
| 100 |
} |
| 101 |
|
| 102 |
for c0 := 0; c0 < ncol; c0++ { |
| 103 |
for c1 := 0; c1 < ncol; c1++ { |
| 104 |
fmt.Printf("%s + %s -> %s\n", colname[c0], colname[c1], colname[complement[c0|c1<<2]]) |
| 105 |
} |
| 106 |
} |
| 107 |
fmt.Print("\n") |
| 108 |
|
| 109 |
pallmall([]int{blue, red, yellow}) |
| 110 |
pallmall([]int{blue, red, yellow, red, yellow, blue, red, yellow, red, blue}) |
| 111 |
} |
| 112 |
|
| 113 |
func pallmall(cols []int) { |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
meetingplace := make(chan rendez, 1) |
| 118 |
meetingplace <- rendez{n: 0} |
| 119 |
|
| 120 |
ended := make(chan result) |
| 121 |
msg := "" |
| 122 |
for i, col := range cols { |
| 123 |
go creature(info{col, i}, meetingplace, ended) |
| 124 |
msg += " " + colname[col] |
| 125 |
} |
| 126 |
fmt.Println(msg) |
| 127 |
tot := 0 |
| 128 |
|
| 129 |
for range cols { |
| 130 |
result := <-ended |
| 131 |
tot += result.met |
| 132 |
fmt.Printf("%v%v\n", result.met, spell(result.same, true)) |
| 133 |
} |
| 134 |
fmt.Printf("%v\n\n", spell(tot, true)) |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
func creature(info0 info, meetingplace chan rendez, ended chan result) { |
| 140 |
c0 := make(chan info) |
| 141 |
met := 0 |
| 142 |
same := 0 |
| 143 |
for { |
| 144 |
var othername int |
| 145 |
|
| 146 |
switch r := <-meetingplace; { |
| 147 |
case r.n >= n: |
| 148 |
|
| 149 |
meetingplace <- rendez{n: r.n} |
| 150 |
ended <- result{met, same} |
| 151 |
return |
| 152 |
case r.mate == nil: |
| 153 |
|
| 154 |
|
| 155 |
meetingplace <- rendez{n: r.n, info: info0, mate: c0} |
| 156 |
info1 := <-c0 |
| 157 |
othername = info1.name |
| 158 |
info0.colour = complement[info0.colour|info1.colour<<2] |
| 159 |
default: |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
r.n++ |
| 164 |
meetingplace <- rendez{n: r.n, mate: nil} |
| 165 |
r.mate <- info0 |
| 166 |
othername = r.info.name |
| 167 |
info0.colour = complement[info0.colour|r.info.colour<<2] |
| 168 |
} |
| 169 |
if othername == info0.name { |
| 170 |
same++ |
| 171 |
} |
| 172 |
met++ |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
var digits = [...]string{"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"} |
| 177 |
|
| 178 |
func spell(n int, required bool) string { |
| 179 |
if n == 0 && !required { |
| 180 |
return "" |
| 181 |
} |
| 182 |
return spell(n/10, false) + " " + digits[n%10] |
| 183 |
} |