From ff454776fab77299932290f76c86cc20db7748fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20M=C3=BCller?= Date: Sun, 22 Dec 2024 22:59:38 +0100 Subject: [PATCH] Add solution for "Day 10: Hoof It", part 2 --- src/HoofIt.cpp | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/HoofIt.cpp b/src/HoofIt.cpp index acec2d2..011c1ab 100644 --- a/src/HoofIt.cpp +++ b/src/HoofIt.cpp @@ -34,22 +34,28 @@ std::string HoofIt::getInputFileName() const void HoofIt::finish() { std::queue queue{}; - std::vector>> trailScoreMap{}; + std::vector>> trailEndsMap{}; + trailEndsMap.reserve(lines.size()); + std::vector> trailScoreMap{}; trailScoreMap.reserve(lines.size()); for (int j = 0; j < lines.size(); j++) { - std::vector> row{}; - row.reserve(lines[j].size()); - trailScoreMap.push_back(row); + std::vector> endsRow{}; + endsRow.reserve(lines[j].size()); + trailEndsMap.push_back(endsRow); + std::vector scoreRow(lines[j].size(), 0); + trailScoreMap.push_back(scoreRow); + for (int i = 0; i < lines[j].size(); i++) { std::vector 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]; } } }