Add solution for "Day 10: Hoof It", part 1
This commit is contained in:
parent
4ed3b84cb6
commit
eb8f7f7221
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <aoc/LinesSolver.hpp>
|
||||||
|
#include <aoc/Point2.hpp>
|
||||||
|
|
||||||
|
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<Point2>& source, std::vector<Point2>& destination);
|
||||||
|
};
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#include <aoc/HoofIt.hpp>
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <queue>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <aoc/Point2.hpp>
|
||||||
|
|
||||||
|
std::string HoofIt::getPuzzleName() const
|
||||||
|
{
|
||||||
|
return "Day 10: Hoof It";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string HoofIt::getInputFileName() const
|
||||||
|
{
|
||||||
|
return "hoof_it.txt";
|
||||||
|
}
|
||||||
|
|
||||||
|
void HoofIt::finish()
|
||||||
|
{
|
||||||
|
std::queue<Point2> queue{};
|
||||||
|
std::vector<std::vector<std::vector<Point2>>> 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);
|
||||||
|
for (int i = 0; i < lines[j].size(); i++)
|
||||||
|
{
|
||||||
|
std::vector<Point2> 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<Point2>& source, std::vector<Point2>& 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -27,6 +27,7 @@
|
||||||
#include <aoc/CeresSearch.hpp>
|
#include <aoc/CeresSearch.hpp>
|
||||||
#include <aoc/PrintQueue.hpp>
|
#include <aoc/PrintQueue.hpp>
|
||||||
#include <aoc/GuardGallivant.hpp>
|
#include <aoc/GuardGallivant.hpp>
|
||||||
|
#include <aoc/HoofIt.hpp>
|
||||||
|
|
||||||
void Program::run()
|
void Program::run()
|
||||||
{
|
{
|
||||||
|
@ -50,6 +51,7 @@ void Program::runSolvers()
|
||||||
runSolver<CeresSearch>(solverEngine);
|
runSolver<CeresSearch>(solverEngine);
|
||||||
runSolver<PrintQueue>(solverEngine);
|
runSolver<PrintQueue>(solverEngine);
|
||||||
runSolver<GuardGallivant>(solverEngine);
|
runSolver<GuardGallivant>(solverEngine);
|
||||||
|
runSolver<HoofIt>(solverEngine);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> Program::getInputPaths() const
|
std::vector<std::string> Program::getInputPaths() const
|
||||||
|
|
Loading…
Reference in New Issue