diff --git a/include/aoc/HoofIt.hpp b/include/aoc/HoofIt.hpp new file mode 100644 index 0000000..ba4b7ad --- /dev/null +++ b/include/aoc/HoofIt.hpp @@ -0,0 +1,34 @@ +// Solutions to the Advent Of Code 2024. +// Copyright (C) 2024 Stefan Müller +// +// This program is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation, either version 3 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// this program. If not, see . + +#pragma once + +#include + +#include +#include + +class HoofIt + : public LinesSolver +{ +public: + std::string getPuzzleName() const override; + std::string getInputFileName() const override; + void finish() override; +private: + char getTrailheadChar() const; + char getTrailTopChar() const; + void addUnique(const std::vector& source, std::vector& destination); +}; diff --git a/src/HoofIt.cpp b/src/HoofIt.cpp new file mode 100644 index 0000000..acec2d2 --- /dev/null +++ b/src/HoofIt.cpp @@ -0,0 +1,113 @@ +// Solutions to the Advent Of Code 2024. +// Copyright (C) 2024 Stefan Müller +// +// This program is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation, either version 3 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with +// this program. If not, see . + +#include + +#include +#include +#include + +#include + +std::string HoofIt::getPuzzleName() const +{ + return "Day 10: Hoof It"; +} + +std::string HoofIt::getInputFileName() const +{ + return "hoof_it.txt"; +} + +void HoofIt::finish() +{ + std::queue queue{}; + 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); + for (int i = 0; i < lines[j].size(); i++) + { + std::vector ends{}; + trailScoreMap[j].push_back(ends); + if (lines[j][i] == getTrailTopChar()) + { + Point2 trailEnd{ i, j }; + trailScoreMap[j][i].push_back(trailEnd); + queue.push(trailEnd); + } + } + } + + while (!queue.empty()) + { + auto trail = queue.front(); + queue.pop(); + if (getPosition(trail) != getTrailheadChar()) + { + for (auto direction : Point2::cardinalDirections) + { + Point2 p{ trail + direction }; + if (isInBounds(p) && getPosition(p) + 1 == getPosition(trail)) + { + if (trailScoreMap[p.y][p.x].empty()) + { + queue.push(p); + } + addUnique(trailScoreMap[trail.y][trail.x], trailScoreMap[p.y][p.x]); + } + } + } + else + { + part1 += trailScoreMap[trail.y][trail.x].size(); + } + } +} + +char HoofIt::getTrailheadChar() const +{ + return '0'; +} + +char HoofIt::getTrailTopChar() const +{ + return '9'; +} + +void HoofIt::addUnique(const std::vector& source, std::vector& destination) +{ + for (auto end : source) + { + auto isUnique{ true }; + size_t i{ 0 }; + while (isUnique && i < destination.size()) + { + if (end == destination[i]) + { + isUnique = false; + } + i++; + } + if (isUnique) + { + destination.push_back(end); + } + } +} diff --git a/src/Program.cpp b/src/Program.cpp index 1b020db..6e3a9e1 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -27,6 +27,7 @@ #include #include #include +#include void Program::run() { @@ -50,6 +51,7 @@ void Program::runSolvers() runSolver(solverEngine); runSolver(solverEngine); runSolver(solverEngine); + runSolver(solverEngine); } std::vector Program::getInputPaths() const