Created
December 25, 2014 17:10
-
-
Save kundzi/37033995dd80a359c984 to your computer and use it in GitHub Desktop.
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 ( | |
| "math/rand" | |
| "fmt" | |
| "time" | |
| ) | |
| type Pack struct { | |
| name string | |
| targets []string | |
| cypher map[string]int | |
| encoded []int | |
| } | |
| func main() { | |
| prizesCount := 2 | |
| names := []string{ | |
| "Дима", | |
| "Саша", | |
| "Aня", | |
| "Настя", | |
| "Ваня", | |
| } | |
| personsCount := len(names) | |
| packs := make([]Pack, personsCount) | |
| for i :=0; i < len(packs); i++ { | |
| packs[i] = Pack { | |
| names[i], | |
| make([]string, 0), | |
| make(map[string]int), | |
| make([]int, prizesCount), | |
| } | |
| } | |
| q := make([]string, personsCount * prizesCount) | |
| for i := 0; i < len(q); i++ { | |
| q[i] = names[i % personsCount] | |
| } | |
| rand.Seed(time.Now().UnixNano()) | |
| for _,person := range q { | |
| i := rand.Intn(len(packs)) | |
| for packs[i].name == person || | |
| len(packs[i].targets) == prizesCount || | |
| contains(packs[i].targets, person) { | |
| // do not make selfgists, or double gifts | |
| i = rand.Intn(len(packs)) | |
| } | |
| packs[i].targets = append(packs[i].targets, person) | |
| } | |
| // create cyphers for each person | |
| for i := range packs { | |
| pack := &packs[i] | |
| pack.cypher = cypher(names) | |
| for i, t := range pack.targets { | |
| pack.encoded[i] = pack.cypher[t] | |
| } | |
| } | |
| for i := range packs { | |
| pack := packs[i] | |
| fmt.Printf("%s -> %s -> %d -> %v\n", | |
| pack.name, | |
| "NOPE",//pack.targets, | |
| pack.encoded, | |
| pack.cypher, | |
| ) | |
| } | |
| } | |
| func contains(ar []string, item string) bool { | |
| for _,v := range ar { | |
| if v == item { | |
| return true | |
| } | |
| } | |
| return false | |
| } | |
| func cypher(names []string) map[string]int { | |
| s := sequence(len(names)) | |
| shuffle(s) | |
| c := make(map[string]int) | |
| for i,v := range names { | |
| c[v] = rand.Intn(42) + s[i] + 1 | |
| } | |
| return c | |
| } | |
| func shuffle(ar []int) { | |
| for i := range ar { | |
| j := rand.Intn(i + 1) | |
| ar[i], ar[j] = ar[j], ar[i] | |
| } | |
| } | |
| func sequence(upto int) []int { | |
| s := make([]int, upto) | |
| for i := range s { | |
| s[i] = i | |
| } | |
| return s | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment