Skip to content

Instantly share code, notes, and snippets.

@brickwall2900
Last active June 17, 2025 14:05
Show Gist options
  • Save brickwall2900/c4d63c385cef899f0b041aedcda5cb26 to your computer and use it in GitHub Desktop.
Save brickwall2900/c4d63c385cef899f0b041aedcda5cb26 to your computer and use it in GitHub Desktop.
genbio groupings haha
package com.github.brickwall2900;
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Person> people = readPeople();
List<List<Person>> groups = group(people);
for (int i = 0; i < groups.size(); i++) {
System.out.printf("Group %d%n", i + 1);
List<Person> group = groups.get(i);
for (Person person : group) {
System.out.printf(" - %s, %s %s%n", person.name.last, person.name.first, person.name.middle);
}
}
}
public static List<List<Person>> group(List<Person> people) {
// okay 5 groups in total
// equal number of boys and girls in groupings
// so 3 boys and 4 girls per group
// except 1 group that is 3 boys and 5 girls
int totalGroups = 5;
int boysPerGroup = (int) (people.stream().filter(p -> p.gender == Gender.BOY).count() / totalGroups);
int girlsPerGroup = (int) (people.stream().filter(p -> p.gender == Gender.GIRL).count() / totalGroups);
List<Person> boysOnly = new ArrayList<>(people.stream().filter(p -> p.gender == Gender.BOY).toList());
List<Person> girlsOnly = new ArrayList<>(people.stream().filter(p -> p.gender == Gender.GIRL).toList());
List<List<Person>> groups = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < totalGroups; i++) {
List<Person> group = new ArrayList<>();
for (int b = 0; b < boysPerGroup; b++) {
int rIndex = random.nextInt(0, boysOnly.size());
group.add(boysOnly.remove(rIndex));
}
for (int g = 0; g < girlsPerGroup; g++) {
int rIndex = random.nextInt(0, girlsOnly.size());
group.add(girlsOnly.remove(rIndex));
}
groups.add(group);
}
// any remainders?
// pick random group then join
while (!boysOnly.isEmpty()) {
int rGroupIndex = random.nextInt(0, groups.size());
List<Person> group = groups.get(rGroupIndex);
group.add(boysOnly.removeFirst());
}
while (!girlsOnly.isEmpty()) {
int rGroupIndex = random.nextInt(0, groups.size());
List<Person> group = groups.get(rGroupIndex);
group.add(girlsOnly.removeFirst());
}
return groups;
}
public static List<Person> readPeople() {
List<Person> people = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(
new BufferedInputStream(Objects.requireNonNull(Main.class.getResourceAsStream("/people.txt")))))) {
String line;
while ((line = reader.readLine()) != null) {
String[] split = line.split("\\|");
String realName = split[0];
String genderString = split[1];
// find gender
Gender gender = null;
for (Gender availableGender : Gender.values()) {
if (genderString.toUpperCase(Locale.ROOT).equals(availableGender.name())) {
gender = availableGender;
break;
}
}
Objects.requireNonNull(gender, "tf");
// parse name
// LastName, FirstName1 FirstName2 MiddleName
int firstComma = realName.indexOf(',');
String lastName = realName.substring(0, firstComma);
// next to firstComma is a space
// find until the last word
// tokenize
String[] nameSplit = realName.split("\\s+");
int lastWordIndex = nameSplit.length - 1;
String middleInitial = nameSplit[lastWordIndex];
int middleInitialStart = realName.indexOf(middleInitial);
String firstName = realName.substring(firstComma + 1, middleInitialStart).trim();
Name name = new Name(firstName, middleInitial, lastName);
people.add(new Person(name, gender));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return people;
}
enum Gender {
BOY, GIRL;
}
public record Name(String first, String middle, String last) {}
public record Person(Name name, Gender gender) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment