d4p2
This commit is contained in:
parent
eac8571ea7
commit
0b2afd5993
1 changed files with 63 additions and 16 deletions
|
|
@ -20,10 +20,10 @@ struct Diagram {
|
||||||
size_t width;
|
size_t width;
|
||||||
size_t height;
|
size_t height;
|
||||||
|
|
||||||
auto mdspan() const { return std::mdspan(data.data(), height, width); }
|
auto mdspan() { return std::mdspan(data.data(), height, width); }
|
||||||
};
|
};
|
||||||
|
|
||||||
auto printMap(const Diagram &diagram) {
|
auto printMap(Diagram &diagram) {
|
||||||
auto mdspan = diagram.mdspan();
|
auto mdspan = diagram.mdspan();
|
||||||
for (auto row = 0UZ; row != mdspan.extent(0); row++) {
|
for (auto row = 0UZ; row != mdspan.extent(0); row++) {
|
||||||
for (auto col = 0UZ; col != mdspan.extent(1); col++) {
|
for (auto col = 0UZ; col != mdspan.extent(1); col++) {
|
||||||
|
|
@ -48,8 +48,7 @@ auto parseMap(const std::string &filename) -> std::expected<Diagram, std::string
|
||||||
while (std::getline(inputF, puzzleLine)) {
|
while (std::getline(inputF, puzzleLine)) {
|
||||||
// std::println("{:3}: {}", diagram.height, puzzleLine);
|
// std::println("{:3}: {}", diagram.height, puzzleLine);
|
||||||
diagram.data.push_back(0);
|
diagram.data.push_back(0);
|
||||||
diagram.data.append_range(puzzleLine | std::views::transform([](char c) -> int { return c == '.' ? 0 : 1; }) |
|
diagram.data.append_range(puzzleLine | std::views::transform([](char c) -> int { return c == '.' ? 0 : 1; }));
|
||||||
std::ranges::to<std::vector<int>>());
|
|
||||||
diagram.data.push_back(0);
|
diagram.data.push_back(0);
|
||||||
diagram.height++;
|
diagram.height++;
|
||||||
|
|
||||||
|
|
@ -62,12 +61,12 @@ auto parseMap(const std::string &filename) -> std::expected<Diagram, std::string
|
||||||
diagram.data.insert_range(diagram.data.begin(), std::vector<int>(diagram.width, 0));
|
diagram.data.insert_range(diagram.data.begin(), std::vector<int>(diagram.width, 0));
|
||||||
diagram.height += 2;
|
diagram.height += 2;
|
||||||
|
|
||||||
printMap(diagram);
|
// printMap(diagram);
|
||||||
// auto ms2 = std::mdspan(diagram.data.data(), diagram.width, diagram.height);
|
// auto ms2 = std::mdspan(diagram.data.data(), diagram.width, diagram.height);
|
||||||
return std::move(diagram);
|
return std::move(diagram);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto countMovablePaper(const Diagram &diagram) -> long {
|
auto countMovablePaper(Diagram &diagram) -> long {
|
||||||
long nMovablePaper{};
|
long nMovablePaper{};
|
||||||
auto mdspan = diagram.mdspan();
|
auto mdspan = diagram.mdspan();
|
||||||
for (auto row = 1UZ; row != mdspan.extent(0) - 1; row++) {
|
for (auto row = 1UZ; row != mdspan.extent(0) - 1; row++) {
|
||||||
|
|
@ -75,7 +74,7 @@ auto countMovablePaper(const Diagram &diagram) -> long {
|
||||||
|
|
||||||
if (mdspan[row, col] == 1) {
|
if (mdspan[row, col] == 1) {
|
||||||
// std::print("checking [{},{}]:", row, col);
|
// std::print("checking [{},{}]:", row, col);
|
||||||
// shitty kernal
|
// shitty kernel
|
||||||
int upLeft = mdspan[row - 1, col - 1];
|
int upLeft = mdspan[row - 1, col - 1];
|
||||||
int up = mdspan[row - 1, col];
|
int up = mdspan[row - 1, col];
|
||||||
int upRight = mdspan[row - 1, col + 1];
|
int upRight = mdspan[row - 1, col + 1];
|
||||||
|
|
@ -100,26 +99,74 @@ auto countMovablePaper(const Diagram &diagram) -> long {
|
||||||
return nMovablePaper;
|
return nMovablePaper;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto moveMoveablePaper(Diagram &diagram) -> long {
|
||||||
|
long totalPaperRemoved{};
|
||||||
|
|
||||||
|
auto mdspan = diagram.mdspan();
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
long currentPaperRemoved = 0;
|
||||||
|
for (auto row = 1UZ; row != mdspan.extent(0) - 1; row++) {
|
||||||
|
for (auto col = 1UZ; col != mdspan.extent(1) - 1; col++) {
|
||||||
|
|
||||||
|
if (mdspan[row, col] == 1) {
|
||||||
|
// std::print("checking [{},{}]:", row, col);
|
||||||
|
|
||||||
|
// shitty kernal
|
||||||
|
int neighbors{};
|
||||||
|
neighbors += mdspan[row - 1, col - 1];
|
||||||
|
neighbors += mdspan[row - 1, col];
|
||||||
|
neighbors += mdspan[row - 1, col + 1];
|
||||||
|
|
||||||
|
neighbors += mdspan[row, col - 1];
|
||||||
|
neighbors += mdspan[row, col + 1];
|
||||||
|
|
||||||
|
neighbors += mdspan[row + 1, col - 1];
|
||||||
|
neighbors += mdspan[row + 1, col];
|
||||||
|
neighbors += mdspan[row + 1, col + 1];
|
||||||
|
|
||||||
|
// caveman shit
|
||||||
|
if (neighbors < 4) {
|
||||||
|
|
||||||
|
mdspan[row, col] = 0;
|
||||||
|
// std::println("Found");
|
||||||
|
currentPaperRemoved++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// std::println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (currentPaperRemoved == 0) {
|
||||||
|
std::println("Didn't remove any more paper this iteration! Stopping.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
totalPaperRemoved += currentPaperRemoved;
|
||||||
|
// printMap(diagram);
|
||||||
|
}
|
||||||
|
|
||||||
|
return totalPaperRemoved;
|
||||||
|
}
|
||||||
|
|
||||||
auto main() -> int {
|
auto main() -> int {
|
||||||
auto testCase = parseMap("test_input");
|
auto testCase = parseMap("test_input");
|
||||||
if (testCase) {
|
if (testCase) {
|
||||||
|
|
||||||
auto testResult = countMovablePaper(*testCase);
|
// auto testResult = countMovablePaper(*testCase);
|
||||||
std::println("P1 Testcase result: {}", testResult);
|
// std::println("P1 Testcase result: {}", testResult);
|
||||||
|
|
||||||
// auto testResultP2 = countMovablePaper(*testCase);
|
auto testResultP2 = moveMoveablePaper(*testCase);
|
||||||
// std::println("P2 Testcase result: {}", testResultP2);
|
std::println("P2 Testcase result: {}", testResultP2);
|
||||||
} else {
|
} else {
|
||||||
std::print("{}\n", testCase.error());
|
std::print("{}\n", testCase.error());
|
||||||
}
|
}
|
||||||
|
|
||||||
auto realPuzzle = parseMap("puzzle_input");
|
auto realPuzzle = parseMap("puzzle_input");
|
||||||
if (realPuzzle) {
|
if (realPuzzle) {
|
||||||
auto realResult = countMovablePaper(*realPuzzle);
|
// auto realResult = countMovablePaper(*realPuzzle);
|
||||||
std::println("P1 Real result: {}", realResult);
|
// std::println("P1 Real result: {}", realResult);
|
||||||
|
|
||||||
// auto realResultP2 = countJoltagesP2(*realPuzzle);
|
auto realResultP2 = moveMoveablePaper(*realPuzzle);
|
||||||
// std::println("P2 Real result: {}", realResultP2);
|
std::println("P2 Real result: {}", realResultP2);
|
||||||
} else {
|
} else {
|
||||||
std::print("{}\n", realPuzzle.error());
|
std::print("{}\n", realPuzzle.error());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue