Created
January 14, 2013 17:47
-
-
Save Koitaro/4531857 to your computer and use it in GitHub Desktop.
Go/goroutine: Project Euler 40-49
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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