Created
November 16, 2018 09:08
-
-
Save rogerwelin/4dc5a7018473e604ab1b7bce0341091f to your computer and use it in GitHub Desktop.
ring
This file contains hidden or 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 ( | |
| "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