Skip to content

Instantly share code, notes, and snippets.

@youssef3wi
Created November 22, 2023 14:04
Show Gist options
  • Select an option

  • Save youssef3wi/cb9bb9bf569e4bd474e0ecf85433b8cf to your computer and use it in GitHub Desktop.

Select an option

Save youssef3wi/cb9bb9bf569e4bd474e0ecf85433b8cf to your computer and use it in GitHub Desktop.
Several microorganisms of different families have been arranged next to each other. Knowing that a microorganism eats its neighbor if it is of a different family and smaller in size, write a program that returns the largest family and its size when the situation reaches an equilibrium.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Several microorganisms of different families have been arranged next to each other. Knowing that a microorganism eats its neighbor if it is of a different family and smaller in size, write a program that returns the largest family and its size when the situation reaches an equilibrium.
*
* <ul>
* Process:
* <li>Each microorganism belongs to a family, defined by a letter, and has a size, defined by an integer.</li>
* <li>At each simulation round, a microorganism can eat a neighbor (directly to the left or to the right) if the latter is from another family and has a stricly smaller size. If there is a target on both sides, the left one is chosen.</li>
* <li>During a round, the resolution is done from left to right. For example, if a microorganism can be eaten by both its left and right neighbors, the left one is chosen.</li>
* <li>When a microorganism eats another, its size increases by the size of its target and the place occupied by the target disappears</li>
* <li>In the same simulation round, a microorganism cannot both eat and eaten. The choice of event (eating or being eaten) is determined by the order of resolution.</li>
* <li>The simulation ends when no more microorganisms can eat others.</li>
* </ul>
* <p>
* At the end of the simulation, take the microorganism with the target size, return a string consisting of its family, a separation space and its size.
*/
public class CodingGame {
public static void main(String[] args) {
// Names of the bacteria families.
List<String> families = new ArrayList<>(Arrays.asList("A", "B", "C", "B", "B", "A", "C"));
// Sizes of the bacteria families.
List<Integer> sizes = new ArrayList<>(Arrays.asList(5, 5, 2, 10, 4, 6, 7));
int idx = 1;
String lastFamily = null;
while (sizes.size() > 1) {
if (idx + 1 < sizes.size() && families.get(idx).equals(lastFamily)) {
idx++;
continue;
}
if (sizes.get(idx - 1) > sizes.get(idx)) {
lastFamily = families.get(idx - 1);
// Update
sizes.set(idx - 1, sizes.get(idx - 1) + sizes.get(idx));
// Remove bacteria
sizes.remove(idx);
families.remove(idx);
} else if (sizes.get(idx) > sizes.get(idx - 1)) {
lastFamily = families.get(idx);
// Update
sizes.set(idx, sizes.get(idx - 1) + sizes.get(idx));
// Remove bacteria
sizes.remove(idx - 1);
families.remove(idx - 1);
}
// Go forward or backward
if (idx + 1 >= sizes.size()) {
idx--;
} else {
idx++;
}
}
// Name and size of the biggest bacteria family.
System.out.printf("WIN: %s %d%n", families.get(0), sizes.get(0));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment