Skip to content

Instantly share code, notes, and snippets.

@rogerwelin
Created November 16, 2018 09:08
Show Gist options
  • Select an option

  • Save rogerwelin/4dc5a7018473e604ab1b7bce0341091f to your computer and use it in GitHub Desktop.

Select an option

Save rogerwelin/4dc5a7018473e604ab1b7bce0341091f to your computer and use it in GitHub Desktop.
ring
package main
import (
"container/ring"
"fmt"
"strconv"
)
func main() {
// should produce 6
// length of length of arr / 2 = number of steps forward
// if pos i equal to pos + steps forward ad it to sum
a := "1212"
sum := 0
length := len(a)
r := ring.New(length)
steps := length / 2
for _, val := range a {
v, _ := strconv.Atoi(string(val))
r.Value = v
r = r.Next()
}
for i := 0; i < r.Len(); i++ {
a := r.Move(steps)
if r.Value == a.Value {
intval := r.Value.(int)
sum += intval
}
r = r.Next()
}
fmt.Println(sum)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment