1. Qiita
  2. 投稿
  3. golang

ズンドコキヨシGolang並列版

  • 5
    いいね
  • 0
    コメント

元ネタ

http://qiita.com/shunsugai@github/items/971a15461de29563bf90

Go並列版

途中経過の出力不可能ですが

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)]
}
Comments Loading...