Skip to content

Commit

Permalink
day11 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
JellevanKraaij committed Dec 11, 2023
1 parent 58414fd commit de0a403
Showing 1 changed file with 49 additions and 27 deletions.
76 changes: 49 additions & 27 deletions day11/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,24 @@ using SpaceCell = Type;
using SpaceRow = vector<SpaceCell>;
using Space = vector<SpaceRow>;

void expandSpace(Space &space) {
// Horizontal
vector<int> calcEmptyRows(const Space &space) {
vector<int> emptyRows;
for (int i = 0; i < space.size(); i++) {
bool hasGalaxy = false;
for (SpaceCell &cell : space[i]) {
for (const SpaceCell &cell : space[i]) {
if (cell == GALAXY) {
hasGalaxy = true;
break;
}
}
if (!hasGalaxy) {
space.insert(space.begin() + i, SpaceRow(space[i].size(), EMPTY));
i++;
}
if (!hasGalaxy)
emptyRows.push_back(i);
}
// Vertical
return emptyRows;
}

vector<int> calcEmptyColumns(const Space &space) {
vector<int> emptyColumns;
for (int i = 0; i < space[0].size(); i++) {
bool hasGalaxy = false;
for (int j = 0; j < space.size(); j++) {
Expand All @@ -36,13 +38,31 @@ void expandSpace(Space &space) {
break;
}
}
if (!hasGalaxy) {
for (auto &row : space) {
row.insert(row.begin() + i, EMPTY);
if (!hasGalaxy)
emptyColumns.push_back(i);
}
return emptyColumns;
}

vector<pair<int, int>> findGalaxies(const Space &space) {
vector<pair<int, int>> galaxies;
for (int i = 0; i < space.size(); i++) {
for (int j = 0; j < space[i].size(); j++) {
if (space[i][j] == GALAXY) {
galaxies.push_back({i, j});
}
i++;
}
}
return galaxies;
}

int passesEmptyRoC(const vector<int> &empty, int start, int end) {
int count = 0;
for (int i = start; i < end; i++) {
if (find(empty.begin(), empty.end(), i) != empty.end())
count++;
}
return count;
}

int main(int argc, char **argv) {
Expand All @@ -68,28 +88,30 @@ int main(int argc, char **argv) {
space.push_back(row);
}

expandSpace(space);
vector<int> emptyRows = calcEmptyRows(space);
vector<int> emptyColumns = calcEmptyColumns(space);

vector<pair<int, int>> galaxies;
vector<pair<int, int>> galaxies = findGalaxies(space);

for (int i = 0; i < space.size(); i++) {
for (int j = 0; j < space[i].size(); j++) {
if (space[i][j] == GALAXY) {
galaxies.push_back({i, j});
}
}
}

int total = 0;
int iteration = 0;
long total = 0;
long total2 = 0;
int galaxyCombinations = 0;
for (auto it = galaxies.begin(); it != galaxies.end(); it++) {
auto &[x1, y1] = *it;
for (auto it2 = it + 1; it2 != galaxies.end(); it2++) {
auto &[x2, y2] = *it2;
total += (max(x1, x2) - min(x1, x2)) + (max(y1, y2) - min(y1, y2));
iteration++;

int totalWithoutExpansion = (max(x1, x2) - min(x1, x2)) + (max(y1, y2) - min(y1, y2));

int emptyRowsCnt = passesEmptyRoC(emptyRows, min(x1, x2), max(x1, x2));
int emptyColumnsCnt = passesEmptyRoC(emptyColumns, min(y1, y2), max(y1, y2));

total += totalWithoutExpansion + emptyRowsCnt + emptyColumnsCnt;
total2 += totalWithoutExpansion + (emptyRowsCnt + emptyColumnsCnt) * 999999;
galaxyCombinations++;
}
}
cout << "Total: " << total << endl;
cout << "GalaxyCount: " << iteration << endl;
cout << "Total2: " << total2 << endl;
cout << "GalaxyCount: " << galaxyCombinations << endl;
}

0 comments on commit de0a403

Please sign in to comment.