Skip to content

Instantly share code, notes, and snippets.

@JamesCullum
Created November 14, 2021 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesCullum/7eda4b1bc10a5d61b43faa55ca9ff7d0 to your computer and use it in GitHub Desktop.
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
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