|
|
@ -29,38 +29,15 @@ Result Day11::Task1() { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
vector<Coord> galaxies; |
|
|
|
auto galaxies = getGalaxies(universe); |
|
|
|
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(getDistanceSum(galaxies)); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return to_string(sum); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Result Day11::Task2() { |
|
|
|
Result Day11::Task2() { |
|
|
|
Universe universe = parseUniverse(); |
|
|
|
Universe universe = parseUniverse(); |
|
|
|
|
|
|
|
|
|
|
|
vector<Coord> galaxies; |
|
|
|
auto galaxies = getGalaxies(universe); |
|
|
|
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; |
|
|
|
const size_t gapSize = 1000000; |
|
|
|
|
|
|
|
|
|
|
@ -94,6 +71,23 @@ Result Day11::Task2() { |
|
|
|
gx += gapSize - 1; |
|
|
|
gx += gapSize - 1; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return to_string(getDistanceSum(galaxies)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint64 Day11::getDistanceSum(const vector<Coord> &galaxies) { |
|
|
|
uint64 sum = 0; |
|
|
|
uint64 sum = 0; |
|
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < galaxies.size(); i++){ |
|
|
|
for (size_t i = 0; i < galaxies.size(); i++){ |
|
|
@ -105,18 +99,15 @@ Result Day11::Task2() { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return to_string(sum); |
|
|
|
return sum; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Day11::Universe Day11::parseUniverse() const { |
|
|
|
vector<Day11::Coord> Day11::getGalaxies(const Day11::Universe &universe) { |
|
|
|
Universe universe; |
|
|
|
vector<Coord> galaxies; |
|
|
|
|
|
|
|
for (size_t y = 0; y < universe.size(); y++){ |
|
|
|
for (const string &line : input){ |
|
|
|
for (size_t x = 0; x < universe[y].size(); x++) |
|
|
|
vector<bool> row(line.size(), false); |
|
|
|
if (universe[y][x]) |
|
|
|
for (size_t index : findAll(line, "#")) |
|
|
|
galaxies.emplace_back(x, y); |
|
|
|
row[index] = true; |
|
|
|
|
|
|
|
universe.push_back(row); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return galaxies; |
|
|
|
return universe; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|