Add solution for "Day 10: Hoof It", part 2

This commit is contained in:
Stefan Müller 2024-12-22 22:59:38 +01:00
parent eb8f7f7221
commit ff454776fa
1 changed files with 17 additions and 9 deletions

View File

@ -34,22 +34,28 @@ std::string HoofIt::getInputFileName() const
void HoofIt::finish()
{
std::queue<Point2> queue{};
std::vector<std::vector<std::vector<Point2>>> trailScoreMap{};
std::vector<std::vector<std::vector<Point2>>> trailEndsMap{};
trailEndsMap.reserve(lines.size());
std::vector<std::vector<int>> trailScoreMap{};
trailScoreMap.reserve(lines.size());
for (int j = 0; j < lines.size(); j++)
{
std::vector<std::vector<Point2>> row{};
row.reserve(lines[j].size());
trailScoreMap.push_back(row);
std::vector<std::vector<Point2>> endsRow{};
endsRow.reserve(lines[j].size());
trailEndsMap.push_back(endsRow);
std::vector<int> scoreRow(lines[j].size(), 0);
trailScoreMap.push_back(scoreRow);
for (int i = 0; i < lines[j].size(); i++)
{
std::vector<Point2> ends{};
trailScoreMap[j].push_back(ends);
trailEndsMap[j].push_back(ends);
if (lines[j][i] == getTrailTopChar())
{
Point2 trailEnd{ i, j };
trailScoreMap[j][i].push_back(trailEnd);
trailEndsMap[j][i].push_back(trailEnd);
trailScoreMap[j][i] = 1;
queue.push(trailEnd);
}
}
@ -66,17 +72,19 @@ void HoofIt::finish()
Point2 p{ trail + direction };
if (isInBounds(p) && getPosition(p) + 1 == getPosition(trail))
{
if (trailScoreMap[p.y][p.x].empty())
if (trailEndsMap[p.y][p.x].empty())
{
queue.push(p);
}
addUnique(trailScoreMap[trail.y][trail.x], trailScoreMap[p.y][p.x]);
addUnique(trailEndsMap[trail.y][trail.x], trailEndsMap[p.y][p.x]);
trailScoreMap[p.y][p.x] += trailScoreMap[trail.y][trail.x];
}
}
}
else
{
part1 += trailScoreMap[trail.y][trail.x].size();
part1 += trailEndsMap[trail.y][trail.x].size();
part2 += trailScoreMap[trail.y][trail.x];
}
}
}