Skip to content

Instantly share code, notes, and snippets.

@15joeybloom
Created March 17, 2023 16:38
Show Gist options
  • Save 15joeybloom/ee8b6bbb5b684537701f4018b37d270d to your computer and use it in GitHub Desktop.
Save 15joeybloom/ee8b6bbb5b684537701f4018b37d270d to your computer and use it in GitHub Desktop.
Validate a siteswap pattern
package main
import (
"errors"
"fmt"
)
func isValidSiteswap(pattern []int) error {
total := 0
numBeats := 0
for _, x := range pattern {
total += x
if numBeats < x {
numBeats = x
}
}
if total%len(pattern) != 0 {
return errors.New(fmt.Sprintf(
"Pattern average is not a whole number"))
}
beats := make([]bool, numBeats)
for i, x := range pattern {
if beats[(i+x)%numBeats] {
return errors.New(fmt.Sprintf(
"Conflict on beat %d", i+x))
} else {
beats[(i+x)%numBeats] = true
}
}
return nil
}
func main() {
isValidSiteswap([]int{5})
// nil
isValidSiteswap([]int{5, 3, 1})
// nil
isValidSiteswap([]int{4, 4, 1})
// nil
isValidSiteswap([]int{4, 4, 4, 1})
// Pattern average is not a whole number
isValidSiteswap([]int{5, 1, 3})
// Conflict on beat 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment