Create generic bounding box function.

This commit is contained in:
2019-12-17 07:28:39 +01:00
parent 1968e4c5b4
commit d0243fcf4d
3 changed files with 22 additions and 19 deletions

View File

@@ -64,17 +64,10 @@ void aoc2019::day11_part2(std::istream &input, std::ostream &output) {
const auto result = simulate(input, true);
// Determine bounding box
using limits = std::numeric_limits<int>;
int left_edge = limits::max(), right_edge = limits::min(), top_edge = limits::max(), bottom_edge = limits::min();
for (auto& entry : result) {
left_edge = std::min(entry.first[0], left_edge);
right_edge = std::max(entry.first[0], right_edge);
top_edge = std::min(entry.first[1], top_edge);
bottom_edge = std::max(entry.first[1], bottom_edge);
}
auto[lower,upper] = aoc2019::bounding_box(result);
for (int y = top_edge; y <= bottom_edge; ++y) {
for (int x = left_edge; x <= right_edge; ++x) {
for (int y = lower[1]; y <= upper[1]; ++y) {
for (int x = lower[0]; x <= upper[0]; ++x) {
if (auto it = result.find({x, y}); it != result.end() && it->second) {
output << '#';
} else {

View File

@@ -41,16 +41,10 @@ namespace {
// Determine bounding box
using limits = std::numeric_limits<int>;
std::int64_t left_edge = limits::max(), right_edge = limits::min(), top_edge = limits::max(), bottom_edge = limits::min();
for (auto& entry : screen) {
left_edge = std::min(entry.first[0], left_edge);
right_edge = std::max(entry.first[0], right_edge);
top_edge = std::min(entry.first[1], top_edge);
bottom_edge = std::max(entry.first[1], bottom_edge);
}
const auto [lower, upper] = aoc2019::bounding_box(screen);
for (auto y = top_edge; y <= bottom_edge; ++y) {
for (auto x = left_edge; x <= right_edge; ++x) {
for (auto y = lower[1]; y <= upper[1]; ++y) {
for (auto x = lower[0]; x <= upper[0]; ++x) {
char c = ' ';
if (auto it = screen.find({x, y}); it != screen.end()) {
switch (it->second) {

View File

@@ -46,6 +46,22 @@ namespace aoc2019 {
return result;
}
};
template<typename ValueType, std::size_t N, typename Ignored>
std::pair<Point<ValueType, N>, Point<ValueType, N>> bounding_box(const std::unordered_map<Point<ValueType, N>, Ignored> &data) {
Point<ValueType, N> lower, upper;
std::fill(lower.begin(), lower.end(), std::numeric_limits<ValueType>::max());
std::fill(upper.begin(), upper.end(), std::numeric_limits<ValueType>::min());
for (auto &entry : data) {
for (int i = 0; i < N; ++i) {
lower[i] = std::min(entry.first[i], lower[i]);
upper[i] = std::max(entry.first[i], upper[i]);
}
}
return {lower, upper};
}
}
namespace std {