Skip to content

Instantly share code, notes, and snippets.

@Koitaro
Created January 14, 2013 17:47
Show Gist options
  • Save Koitaro/4531857 to your computer and use it in GitHub Desktop.
Save Koitaro/4531857 to your computer and use it in GitHub Desktop.
Go/goroutine: Project Euler 40-49
package main
import (
"fmt"
"io/ioutil"
"runtime"
"strings"
"time"
)
func init() {
runtime.GOMAXPROCS(16)
}
func isTriangle(s string) bool {
s = strings.Trim(s, "\"")
n := 0
for _, v := range s {
n += int(v) - 'A' + 1
}
for i := 1; n > 0; i++ {
n -= i
}
return n == 0
}
type queue struct {
ch chan bool
length int
}
func newQueue() queue {
ch, ps := make(chan bool, 2000), 0
bs, _ := ioutil.ReadFile("words.txt")
for _, v := range strings.Split(string(bs), ",") {
go func(str string) {
if isTriangle(str) {
ch <- true
} else {
ch <- false
}
}(v)
ps++
}
return queue{ch, ps}
}
func problem42() {
answer := 0
for q := newQueue(); q.length > 0; q.length-- {
if <-q.ch {
answer++
}
}
fmt.Println(answer)
}
func main() {
start := time.Now()
problem42()
fmt.Println(time.Now().Sub(start).Seconds())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment