Skip to content

Instantly share code, notes, and snippets.

@royvanrijn
Last active August 29, 2015 14:05
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 royvanrijn/1ea400f32d0f2e9f0422 to your computer and use it in GitHub Desktop.
Save royvanrijn/1ea400f32d0f2e9f0422 to your computer and use it in GitHub Desktop.
De iets meer leesbare versie
import java.util.Arrays;
public class SplashPaint {
public static void main(String[] args) {
// Splits de input op in één array:
String[] square = args[0].split(",");
// Makkelijk voor testen, elk vierkant is goed
int size = (int) Math.sqrt(square.length);
// We slaan de connecties tussen de groepen op
int[] groupConnections = new int[size * size];
// En de groep-index van elke positie:
int[] groupIndex = new int[size * size];
// En het aantal in elke groep:
int[] groupCount = new int[size * size];
// Initialiseer met -1 = onbekend:
Arrays.fill(groupConnections, -1);
Arrays.fill(groupIndex, -1);
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
int index = (x * size) + y;
// Als de vorige een groep heeft, dan horen we bij dezelfde groep:
int currentGroup = (y > 0) && groupIndex[index - 1] != -1 ? groupIndex[index - 1] : index;
if (square[index].equals("X")) {
// We kijken naar de regel boven ons (-1,0,+1)
for (int z = Math.max(0, y - 1); z < Math.min(size, y + 2); z++) {
int e = (x - 1) * size + z;
// Hoort hier eentje van bij een groep? Koppel deze groepen dan:
if (x > 0 && groupIndex[e] > -1) {
groupConnections[groupIndex[e]] = currentGroup = Math.min(currentGroup, groupIndex[e]);
}
}
// Tel deze X op bij de laagst bekende groep:
groupCount[groupConnections[index] = groupIndex[index] = currentGroup]++;
}
}
}
//Fix bug:
for (int i = size * size; i-- > 1;) {
if((groupConnections[i]*groupConnections[i-1])>0 && groupConnections[i]<groupConnections[i-1]) {
groupConnections[i-1]=groupConnections[i];
}
}
// Nu lopen we over alle groepen heen, zijn er groepen die uiteindelijk een connectie hebben?
// Dan updaten we de count van de eerste groep:
for (int i = 0; i < size * size; i++) {
if (groupConnections[i] >= 0 && groupConnections[i] < i) {
// Tel bij de eerste groep de huidige groep-count op:
groupCount[groupConnections[i]] += groupCount[i];
// En zet deze groep op 0
groupCount[i] = 0;
}
}
System.out.println(Arrays.toString(groupConnections));
// Nu zoeken we de kleinste groep:
int smallestGroup = -1;
for (int i = 0; i < size * size; i++) {
if (groupCount[i] > 0) {
smallestGroup = smallestGroup == -1 ? i : groupCount[i] < groupCount[smallestGroup] ? i : smallestGroup;
}
}
// Nu kunnen we simpel over alle xy's lopen, als het coordinaat hoort bij de kleinste groep, print:
String q = groupCount[smallestGroup] + ":";
for (int i = 0; i < size * size; i++) {
if (groupIndex[i] >= 0 && groupConnections[groupIndex[i]] == smallestGroup) {
q += "(" + i % size + "," + i / size + ")";
}
}
System.out.print(q);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment