I'm not proud of this, but it works. lol. (day 8).

This commit is contained in:
David Vereb 2022-12-11 16:59:56 -05:00
parent 39111a9d6a
commit 575b9e8aed

View File

@ -29,132 +29,112 @@ int main()
trees.back().push_back(ch - '0'); trees.back().push_back(ch - '0');
} }
total += 2 * trees.size();
total += 2 * (trees[0].size() - 2);
std::set<std::pair<int, int>> visible_coordinates; std::set<std::pair<int, int>> visible_coordinates;
for(auto y = 0; y < trees.size(); ++y)
// NOTE(dev):
#warning This shit is wrong because it's only finding the FIRST visible tree, not all visible trees.
#warning e.g. If you have 2003005003007004005
#warning ^ ^ ^ ^ are all visible from the left
#warning ^ is visible from the right
// left to right, right to left IN EACH ROW:
for(auto y = 1; y < trees.size() - 1; ++y)
{ {
auto x = 1; visible_coordinates.insert({0,y});
bool found_from_left = false; visible_coordinates.insert({trees[y].size()-1,y});
while(x < trees[y].size() - 1)
{
std::cout << trees[y][x];
if(trees[y][x] < trees[y][x-1])
break;
found_from_left = true;
++x;
}
if(found_from_left)
{
// make sure it isn't the right-most, as that's already counted
if(x == trees[y].size() - 1)
{
if(trees[y][x] < trees[y][x-1])
{
std::cout << ": insert {" << (x - 1) << "," << y << "}"
<< "(" << trees[y][x-1] << ")";
visible_coordinates.insert({x - 1, y});
}
}
else
{
std::cout << ": insert {" << (x - 1) << "," << y << "}"
<< "(" << trees[y][x-1] << ")";
visible_coordinates.insert({x - 1, y});
// NOTE(dev): Only search from the right if there's still room to search:
bool found_from_right = false;
auto found_x = x - 1;
x = trees[y].size() - 2;
while(x > found_x)
{
if(trees[y][x] < trees[y][x + 1])
break;
found_from_right = true;
--x;
}
if(found_from_right)
{
std::cout << ": insert {" << (x + 1) << "," << y << "}"
<< "(" << trees[y][x+1] << ")";
visible_coordinates.insert({x + 1, y});
}
}
}
std::cout << std::endl;
} }
// top to bottom, bottom to top IN EACH COLUMN:
for(auto x = 1; x < trees[0].size() - 1; ++x) for(auto x = 1; x < trees[0].size() - 1; ++x)
{ {
auto y = 1; visible_coordinates.insert({x,0});
bool found_from_top = false; visible_coordinates.insert({x,trees.size()-1});
while(y < trees.size() - 1) }
{
if(trees[y][x] < trees[y-1][x])
break;
found_from_top = true;
++x;
}
if(found_from_top)
{
// make sure it isn't the bottom-most, as that's already counted
if(y == trees.size() - 1)
{
if(trees[y][x] < trees[y-1][x])
{
std::cout << ": insert {" << x << "," << (y - 1) << "}"
<< "(" << trees[y-1][x] << ")";
visible_coordinates.insert({x, y - 1});
}
}
else
{
std::cout << ": insert {" << x << "," << (y - 1) << "}"
<< "(" << trees[y-1][x] << ")";
visible_coordinates.insert({x, y - 1});
// NOTE(dev): Only search from the bottom if there's still room to search: // left to right, right to left IN EACH ROW:
bool found_from_bottom = false; std::vector<int> points;
auto found_y = y - 1; for(auto y = 1; y < trees.size() - 1; ++y)
y = trees.size() - 2; {
while(y > found_y) points.clear();
{
if(trees[y][x] < trees[y + 1][x]) // initialize list with the first value
break; points.push_back(trees[y][0]);
found_from_bottom = true;
--y; // iterate through the rest:
} for(auto x = 1; x < trees[y].size(); ++x)
if(found_from_bottom) {
{ // if it's higher than the most recent one:
std::cout << ": insert {" << x << "," << (y + 1) << "}" const auto val = trees[y][x];
<< "(" << trees[y+1][x] << ")"; if(val > points.back())
visible_coordinates.insert({x, y + 1}); {
} points.push_back(val);
visible_coordinates.insert({x,y});
}
}
}
for(auto y = 1; y < trees.size() - 1; ++y)
{
points.clear();
// initialize list with the first value
points.push_back(trees[y][trees.size()-1]);
// iterate through the rest:
for(auto x = trees[y].size() - 2; x > 0; --x)
{
// if it's higher than the most recent one:
const auto val = trees[y][x];
if(val > points.back())
{
points.push_back(val);
visible_coordinates.insert({x,y});
}
}
}
for(auto x = 1; x < trees[0].size() - 1; ++x)
{
points.clear();
// initialize list with the first value
points.push_back(trees[0][x]);
// iterate through the rest:
for(auto y = 1; y < trees.size(); ++y)
{
// if it's higher than the most recent one:
const auto val = trees[y][x];
if(val > points.back())
{
points.push_back(val);
visible_coordinates.insert({x,y});
}
}
}
for(auto x = 1; x < trees[0].size() - 1; ++x)
{
points.clear();
// initialize list with the first value
points.push_back(trees[trees.size()-1][x]);
// iterate through the rest:
for(auto y = trees.size()-2; y > 0; --y)
{
// if it's higher than the most recent one:
const auto val = trees[y][x];
if(val > points.back())
{
points.push_back(val);
visible_coordinates.insert({x,y});
} }
} }
} }
total += visible_coordinates.size(); total += visible_coordinates.size();
// // track interior for(auto y = 0; y < trees.size(); ++y)
// for(auto y = 1; y < trees.size() - 1; ++y) {
// { for(auto x = 0; x < trees[y].size(); ++x)
// // left & right, first {
// for(auto x = 1; x < trees[y].size() - 1; ++x) if(visible_coordinates.contains({x,y}))
// { std::cout << "\033[1;31m";
else
// } std::cout << "\033[0m";
// } std::cout << trees[y][x];
}
std::cout << std::endl;
}
std::cout << "\033[0m";
std::cout << " Total: " << total << std::endl; std::cout << " Total: " << total << std::endl;
std::cout << "PT2 Total: " << total_pt2 << std::endl; std::cout << "PT2 Total: " << total_pt2 << std::endl;