mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-26 05:10:32 +01:00
Create generic bounding box function.
This commit is contained in:
@@ -64,17 +64,10 @@ void aoc2019::day11_part2(std::istream &input, std::ostream &output) {
|
|||||||
const auto result = simulate(input, true);
|
const auto result = simulate(input, true);
|
||||||
|
|
||||||
// Determine bounding box
|
// Determine bounding box
|
||||||
using limits = std::numeric_limits<int>;
|
auto[lower,upper] = aoc2019::bounding_box(result);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int y = top_edge; y <= bottom_edge; ++y) {
|
for (int y = lower[1]; y <= upper[1]; ++y) {
|
||||||
for (int x = left_edge; x <= right_edge; ++x) {
|
for (int x = lower[0]; x <= upper[0]; ++x) {
|
||||||
if (auto it = result.find({x, y}); it != result.end() && it->second) {
|
if (auto it = result.find({x, y}); it != result.end() && it->second) {
|
||||||
output << '#';
|
output << '#';
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -41,16 +41,10 @@ namespace {
|
|||||||
// Determine bounding box
|
// Determine bounding box
|
||||||
using limits = std::numeric_limits<int>;
|
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();
|
const auto [lower, upper] = aoc2019::bounding_box(screen);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto y = top_edge; y <= bottom_edge; ++y) {
|
for (auto y = lower[1]; y <= upper[1]; ++y) {
|
||||||
for (auto x = left_edge; x <= right_edge; ++x) {
|
for (auto x = lower[0]; x <= upper[0]; ++x) {
|
||||||
char c = ' ';
|
char c = ' ';
|
||||||
if (auto it = screen.find({x, y}); it != screen.end()) {
|
if (auto it = screen.find({x, y}); it != screen.end()) {
|
||||||
switch (it->second) {
|
switch (it->second) {
|
||||||
|
|||||||
@@ -46,6 +46,22 @@ namespace aoc2019 {
|
|||||||
return result;
|
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 {
|
namespace std {
|
||||||
|
|||||||
Reference in New Issue
Block a user