From c90d2383059cc69c5fdd5da94e91dd28dab09bb3 Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Wed, 20 Dec 2023 16:09:37 +0100 Subject: [PATCH] Day 11 --- src/days/11/Day11.cpp | 119 ++++++++++++++++++++++++++++++++++++++++-- src/days/11/Day11.h | 3 ++ 2 files changed, 119 insertions(+), 3 deletions(-) diff --git a/src/days/11/Day11.cpp b/src/days/11/Day11.cpp index 8376f31..515e743 100644 --- a/src/days/11/Day11.cpp +++ b/src/days/11/Day11.cpp @@ -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(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 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(); -} \ No newline at end of file + Universe universe = parseUniverse(); + + vector 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 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 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 row(line.size(), false); + for (size_t index : findAll(line, "#")) + row[index] = true; + universe.push_back(row); + } + + return universe; +} diff --git a/src/days/11/Day11.h b/src/days/11/Day11.h index 29ebeba..c37e70a 100644 --- a/src/days/11/Day11.h +++ b/src/days/11/Day11.h @@ -3,6 +3,9 @@ #include "../../Day.h" class Day11 : public Day { + typedef pair Coord; + typedef vector> Universe; + Universe parseUniverse() const; protected: Result Task1() override;