main
Benjamin Kraft 9 months ago
parent 1e991c1f77
commit c90d238305
  1. 119
      src/days/11/Day11.cpp
  2. 3
      src/days/11/Day11.h

@ -1,9 +1,122 @@
#include "Day11.h"
Result Day11::Task1() {
return Day::Task1();
Universe universe = parseUniverse();
// expand vertically
for (size_t y = 0; y < universe.size(); y++){
auto row = universe[y];
if (!*std::max_element(row.begin(), row.end())){
y++;
auto iter = universe.begin();
std::advance(iter, y);
universe.insert(iter, vector<bool>(row.size()));
}
}
// expand horizontally
for (size_t x = 0; x < universe[0].size(); x++){
bool galaxyFound = false;
for (auto &row : universe)
galaxyFound = std::max(galaxyFound, bool(row[x]));
if (!galaxyFound){
x++;
for (auto &row : universe){
auto iter = row.begin();
std::advance(iter, x);
row.insert(iter, false);
}
}
}
vector<Coord> galaxies;
for (size_t y = 0; y < universe.size(); y++){
auto row = universe[y];
for (size_t x = 0; x < row.size(); x++)
if (universe[y][x])
galaxies.emplace_back(x, y);
}
uint64 sum = 0;
for (size_t i = 0; i < galaxies.size(); i++){
for (size_t j = i + 1; j < galaxies.size(); j++){
auto &[x1, y1] = galaxies[i];
auto &[x2, y2] = galaxies[j];
sum += std::abs(long(x2 - x1)) + std::abs(long(y2 - y1));
}
}
return to_string(sum);
}
Result Day11::Task2() {
return Day::Task2();
}
Universe universe = parseUniverse();
vector<Coord> galaxies;
for (size_t y = 0; y < universe.size(); y++){
auto row = universe[y];
for (size_t x = 0; x < row.size(); x++)
if (universe[y][x])
galaxies.emplace_back(x, y);
}
const size_t gapSize = 1000000;
// expand vertically
vector<size_t> rowExpansions;
for (size_t y = 0; y < universe.size(); y++){
auto row = universe[y];
if (!*std::max_element(row.begin(), row.end()))
rowExpansions.push_back(y);
}
std::reverse(rowExpansions.begin(), rowExpansions.end());
for (size_t y : rowExpansions){
for (auto &[gx, gy] : galaxies)
if (gy > y)
gy += gapSize - 1;
}
// expand horizontally
vector<size_t> columnExpansions;
for (size_t x = 0; x < universe[0].size(); x++){
bool galaxyFound = false;
for (auto &row : universe)
galaxyFound = std::max(galaxyFound, bool(row[x]));
if (!galaxyFound)
columnExpansions.push_back(x);
}
std::reverse(columnExpansions.begin(), columnExpansions.end());
for (size_t x : columnExpansions){
for (auto &[gx, gy] : galaxies)
if (gx > x)
gx += gapSize - 1;
}
uint64 sum = 0;
for (size_t i = 0; i < galaxies.size(); i++){
for (size_t j = i + 1; j < galaxies.size(); j++){
auto &[x1, y1] = galaxies[i];
auto &[x2, y2] = galaxies[j];
sum += std::abs(long(x2 - x1)) + std::abs(long(y2 - y1));
}
}
return to_string(sum);
}
Day11::Universe Day11::parseUniverse() const {
Universe universe;
for (const string &line : input){
vector<bool> row(line.size(), false);
for (size_t index : findAll(line, "#"))
row[index] = true;
universe.push_back(row);
}
return universe;
}

@ -3,6 +3,9 @@
#include "../../Day.h"
class Day11 : public Day {
typedef pair<size_t, size_t> Coord;
typedef vector<vector<bool>> Universe;
Universe parseUniverse() const;
protected:
Result Task1() override;

Loading…
Cancel
Save