Skip to content

Instantly share code, notes, and snippets.

@eklitzke
Created February 17, 2019 06:52
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 eklitzke/6aa6d016096079db592bc2d615a4f041 to your computer and use it in GitHub Desktop.
Save eklitzke/6aa6d016096079db592bc2d615a4f041 to your computer and use it in GitHub Desktop.
package main
import "fmt"
func isValid(x int) bool {
s := 0
for y := 1; y <= 0x2000; y <<= 1 {
if x&y == 0 {
// 0-bit means open paren, push the stack
s++
} else {
// 1-bit means close paren, pop the stack
s--
if s < 0 {
// detect invalid stack
return false
}
}
}
return s == 0
}
func main() {
c := 0
for i := 0; i <= 0x3fff; i++ {
if isValid(i) {
c++
}
}
fmt.Println(c)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment