Ad
Code
Diff
  • public class Kata {
      public static boolean[] populateBathroom(boolean[] urinals, int numOfPeople) {
        // Implement the urinal problem, described as "given x number of people walking 
        // into a bathroom that contains y number of urinals, describe the optimate
        // placement of people so that none is standing next to the other"
        // In this case, the urinals are represented by a boolean array with 
        // true representing a person at a urinal, and false meaning the urinal is empty
        if (urinals.length < numOfPeople) {
          throw new RuntimeException("Cursed input data.");
        }
        int peopleRemain = numOfPeople;
        int peoplePlacePointer = 0;
        boolean[] result = urinals;
        while(peopleRemain > 0) {
          if (peopleRemain == 1) {
            // only one person is not at the urinal
            // we'll place him to far corner of the toilet :)
            result[urinals.length - 1] = true;
            peopleRemain--;
          } else {
            // if more than one person is not at the urinal
            // place first of them at the urinal with number peoplePlacePointer
            result[peoplePlacePointer] = true;
            // then increments the peoplePlacePoiner by value 
            // which depends on empty urinals count from rigth side of peoplePlacePointer and remain people count
            // the point is to distribute resulting empty urinals so that almost equals spaces will be between people
            peoplePlacePointer += 1 + (urinals.length - peoplePlacePointer) / peopleRemain;
            // one less left
            peopleRemain--;
          }
        }
        return result;
      }
    }
    • public class Kata {
    • public static boolean[] populateBathroom(boolean[] urinals, int numOfPeople) {
    • // Implement the urinal problem, described as "given x number of people walking
    • // into a bathroom that contains y number of urinals, describe the optimate
    • // placement of people so that none is standing next to the other"
    • // In this case, the urinals are represented by a boolean array with
    • // true representing a person at a urinal, and false meaning the urinal is empty
    • return new boolean[] {true, false, true};
    • if (urinals.length < numOfPeople) {
    • throw new RuntimeException("Cursed input data.");
    • }
    • int peopleRemain = numOfPeople;
    • int peoplePlacePointer = 0;
    • boolean[] result = urinals;
    • while(peopleRemain > 0) {
    • if (peopleRemain == 1) {
    • // only one person is not at the urinal
    • // we'll place him to far corner of the toilet :)
    • result[urinals.length - 1] = true;
    • peopleRemain--;
    • } else {
    • // if more than one person is not at the urinal
    • // place first of them at the urinal with number peoplePlacePointer
    • result[peoplePlacePointer] = true;
    • // then increments the peoplePlacePoiner by value
    • // which depends on empty urinals count from rigth side of peoplePlacePointer and remain people count
    • // the point is to distribute resulting empty urinals so that almost equals spaces will be between people
    • peoplePlacePointer += 1 + (urinals.length - peoplePlacePointer) / peopleRemain;
    • // one less left
    • peopleRemain--;
    • }
    • }
    • return result;
    • }
    • }