Refactor, but did it get better?
This commit is contained in:
parent
575b9e8aed
commit
4b86074a06
134
2022/8/main.cpp
134
2022/8/main.cpp
@ -4,6 +4,54 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
std::vector<int> ParseRow(auto begin, auto end)
|
||||
{
|
||||
std::vector<int> rtn_indexes;
|
||||
std::vector<int> values;
|
||||
|
||||
// initialize list with the first value
|
||||
values.push_back(*begin);
|
||||
|
||||
// iterate through the rest:
|
||||
for(auto i = begin + 1; i != end; ++i)
|
||||
{
|
||||
|
||||
// if it's higher than the most recent one:
|
||||
const auto val = *i;
|
||||
if(val > values.back())
|
||||
{
|
||||
values.push_back(val);
|
||||
rtn_indexes.push_back(std::distance(begin, i));
|
||||
}
|
||||
}
|
||||
|
||||
return rtn_indexes;
|
||||
}
|
||||
|
||||
std::vector<int> ParseCol(auto begin, auto end, int col)
|
||||
{
|
||||
std::vector<int> rtn_indexes;
|
||||
std::vector<int> values;
|
||||
|
||||
// initialize list with the first value
|
||||
values.push_back((*begin)[col]);
|
||||
|
||||
// iterate through the rest:
|
||||
for(auto i = begin + 1; i != end; ++i)
|
||||
{
|
||||
|
||||
// if it's higher than the most recent one:
|
||||
const auto val = (*i)[col];
|
||||
if(val > values.back())
|
||||
{
|
||||
values.push_back(val);
|
||||
rtn_indexes.push_back(std::distance(begin, i));
|
||||
}
|
||||
}
|
||||
|
||||
return rtn_indexes;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::ifstream ifs("data.txt");
|
||||
@ -45,79 +93,27 @@ int main()
|
||||
std::vector<int> points;
|
||||
for(auto y = 1; y < trees.size() - 1; ++y)
|
||||
{
|
||||
points.clear();
|
||||
// left to right:
|
||||
points = ParseRow(trees[y].begin(), trees[y].end());
|
||||
for(const auto &point : points)
|
||||
visible_coordinates.insert({point,y});
|
||||
|
||||
// initialize list with the first value
|
||||
points.push_back(trees[y][0]);
|
||||
|
||||
// iterate through the rest:
|
||||
for(auto x = 1; x < trees[y].size(); ++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});
|
||||
}
|
||||
}
|
||||
// right to left:
|
||||
points = ParseRow(trees[y].rbegin(), trees[y].rend());
|
||||
for(const auto &point : points)
|
||||
visible_coordinates.insert({trees[y].size() - point - 1,y});
|
||||
}
|
||||
for(auto y = 1; y < trees.size() - 1; ++y)
|
||||
for(auto x = 1; x < trees.size() - 1; ++x)
|
||||
{
|
||||
points.clear();
|
||||
// top to bottom:
|
||||
points = ParseCol(trees.begin(), trees.end(), x);
|
||||
for(const auto &point : points)
|
||||
visible_coordinates.insert({x,point});
|
||||
|
||||
// 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});
|
||||
}
|
||||
}
|
||||
// bottom to top:
|
||||
points = ParseCol(trees.rbegin(), trees.rend(), x);
|
||||
for(const auto &point : points)
|
||||
visible_coordinates.insert({x, trees.size() - point - 1});
|
||||
}
|
||||
|
||||
total += visible_coordinates.size();
|
||||
|
Loading…
Reference in New Issue
Block a user