Skip to content

Instantly share code, notes, and snippets.

@lon9
Created March 16, 2016 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lon9/3a1299fa670e3c708bdd to your computer and use it in GitHub Desktop.
Save lon9/3a1299fa670e3c708bdd to your computer and use it in GitHub Desktop.
ズンドコキヨシGolang並列版 ref: http://qiita.com/Rompei/items/bfa03fbc9a94a37703bb
package main
import (
"fmt"
"math/rand"
"runtime"
"sync"
"time"
)
// Zundoko is ズンドコ配列
var Zundoko = [2]string{"ズン", "ドコ"}
func main() {
cpus := runtime.NumCPU()
runtime.GOMAXPROCS(cpus)
kiyoshiCh := make(chan bool, 1)
var m sync.Mutex
for i := 0; i < cpus; i++ {
go zndk(kiyoshiCh, &m)
}
<-kiyoshiCh
close(kiyoshiCh)
fmt.Println("キ・ヨ・シ!")
}
func zndk(kiyoshiCh chan bool, m *sync.Mutex) {
zdk := make([]string, 5)
for {
for i := 0; i < 5; i++ {
zdk[i] = getZndk(m)
}
if zdk[0] == "ズン" && zdk[1] == "ズン" && zdk[2] == "ズン" && zdk[3] == "ズン" && zdk[4] == "ドコ" {
for _, s := range zdk {
fmt.Println(s)
}
kiyoshiCh <- true
break
}
}
}
func getZndk(m *sync.Mutex) string {
m.Lock()
defer m.Unlock()
rand.Seed(time.Now().UnixNano())
return Zundoko[rand.Intn(2)]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment