Created
November 14, 2021 11:26
-
-
Save JamesCullum/7eda4b1bc10a5d61b43faa55ca9ff7d0 to your computer and use it in GitHub Desktop.
Simple algorithm in Golang to sort people into rooms/groups: https://play.golang.org/p/oLZZ01dyI5o
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 ( | |
"fmt" | |
"math" | |
"strconv" | |
"strings" | |
) | |
func main() { | |
rooms := 3 | |
leftAdults, leftChilds := 6, 2 | |
pplPerRoom := int(math.Ceil(float64(leftAdults+leftChilds) / float64(rooms))) | |
var results []string | |
for i := 1; i <= rooms; i++ { | |
thisRoomA := pplPerRoom | |
thisRoomC := 0 | |
if thisRoomA > leftAdults { | |
// 2 adults left, 3 should be in room - try fill in 1 child | |
thisRoomC = pplPerRoom - leftAdults | |
thisRoomA = thisRoomA - thisRoomC | |
if thisRoomC > leftChilds { | |
thisRoomC = leftChilds | |
} | |
} | |
leftAdults = leftAdults - thisRoomA | |
leftChilds = leftChilds - thisRoomC | |
var roomInhabitants []string | |
for o := 0; o < thisRoomA; o++ { | |
roomInhabitants = append(roomInhabitants, "A") | |
} | |
for o := 0; o < thisRoomC; o++ { | |
roomInhabitants = append(roomInhabitants, "12") | |
} | |
results = append(results, "room"+strconv.Itoa(i)+"="+strings.Join(roomInhabitants, ",")) | |
} | |
fmt.Println(results) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment