Advent-of-Code/2022/8/main.cpp

164 lines
3.8 KiB
C++

#include <fstream>
#include <iostream>
#include <set>
#include <string>
#include <vector>
int main()
{
std::ifstream ifs("data.txt");
if(!ifs.is_open())
{
std::cerr << "Missing data.txt." << std::endl;
return -1;
}
unsigned long total = 0;
unsigned long total_pt2 = 0;
std::vector<std::vector<int>> trees;
// Parse:
for(std::string line; std::getline(ifs, line); )
{
if(line == "")
continue;
trees.push_back({});
for(const char &ch : line)
trees.back().push_back(ch - '0');
}
total += 2 * trees.size();
total += 2 * (trees[0].size() - 2);
std::set<std::pair<int, int>> visible_coordinates;
// 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;
bool found_from_left = false;
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)
{
auto y = 1;
bool found_from_top = false;
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:
bool found_from_bottom = false;
auto found_y = y - 1;
y = trees.size() - 2;
while(y > found_y)
{
if(trees[y][x] < trees[y + 1][x])
break;
found_from_bottom = true;
--y;
}
if(found_from_bottom)
{
std::cout << ": insert {" << x << "," << (y + 1) << "}"
<< "(" << trees[y+1][x] << ")";
visible_coordinates.insert({x, y + 1});
}
}
}
}
total += visible_coordinates.size();
// // track interior
// for(auto y = 1; y < trees.size() - 1; ++y)
// {
// // left & right, first
// for(auto x = 1; x < trees[y].size() - 1; ++x)
// {
// }
// }
std::cout << " Total: " << total << std::endl;
std::cout << "PT2 Total: " << total_pt2 << std::endl;
return 0;
}