public int partOne() {
// Sammle alle Spalten, die nur Punkte enthalten
ArrayList<Integer> emptyColumns = new ArrayList<Integer>();
for (int x = 0; x < inputLines.get(0).length(); x++) {
boolean columnEmpty = true;
for (int y = 0; y < inputLines.size(); y++) {
if (inputLines.get(y).charAt(x) != '.') {
columnEmpty = false;
break;
}
}
if (columnEmpty) {
emptyColumns.add(x + emptyColumns.size());
}
}
ArrayList<int[]> galaxies = new ArrayList<int[]>();
int y = 0;
for (String line: inputLines) {
char[] lineAsChars = line.toCharArray();
// prüfe, ob zeile nur '.' enthält
boolean zeileLeer = true;
for (char c: lineAsChars) {
if (c != '.') {
zeileLeer = false;
break;
}
}
if (zeileLeer) {
y++;
}
int x = 0;
for (char c: lineAsChars) {
if (emptyColumns.contains(x)) {
x++;
}
if (c == '#') {
galaxies.add(new int[]{x, y});
}
x++;
}
y++;
}
int distances = 0;
int skip = 1;
for (int[] g1: galaxies) {
for (int i = skip; i < galaxies.size(); i++) {
int[] g2 = galaxies.get(i);
distances += Math.abs(g1[0] - g2[0]) + Math.abs(g1[1] - g2[1]);
}
skip++;
}
return distances;
}
public long partTwo() {
// Sammle alle Spalten, die nur Punkte enthalten
ArrayList<Integer> emptyColumns = new ArrayList<Integer>();
for (int x = 0; x < inputLines.get(0).length(); x++) {
boolean columnEmpty = true;
for (int y = 0; y < inputLines.size(); y++) {
if (inputLines.get(y).charAt(x) != '.') {
columnEmpty = false;
break;
}
}
if (columnEmpty) {
emptyColumns.add(x + emptyColumns.size() * 999999);
}
}
ArrayList<long[]> galaxies = new ArrayList<long[]>();
long y = 0;
for (String line: inputLines) {
char[] lineAsChars = line.toCharArray();
// prüfe, ob zeile nur '.' enthält
boolean zeileLeer = true;
for (char c: lineAsChars) {
if (c != '.') {
zeileLeer = false;
break;
}
}
if (zeileLeer) {
y += 999999;
}
int x = 0;
for (char c: lineAsChars) {
if (emptyColumns.contains(x)) {
x += 999999;
}
else if (c == '#') {
galaxies.add(new long[]{x, y});
}
x++;
}
y++;
}
long distances = 0;
int skip = 1;
for (long[] g1: galaxies) {
for (int i = skip; i < galaxies.size(); i++) {
long[] g2 = galaxies.get(i);
distances += Math.abs(g1[0] - g2[0]) + Math.abs(g1[1] - g2[1]);
}
skip++;
}
return distances;
}