LoginSignup
5
6

More than 5 years have passed since last update.

ズンドコキヨシGolang並列版

Posted at

元ネタ

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)]
}
5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6