Add solution for "Day 23: LAN Party", part 1
This commit is contained in:
46
src/Graph.cpp
Normal file
46
src/Graph.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
// 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/Graph.hpp>
|
||||
|
||||
#include <memory>
|
||||
|
||||
const std::map<std::string, std::shared_ptr<Vertex>>& Graph::getVertices() const
|
||||
{
|
||||
return vertices_;
|
||||
}
|
||||
|
||||
void Graph::addEdge(const std::string& vertexId1, const std::string& vertexId2)
|
||||
{
|
||||
auto v1 = findOrAddVertex(vertexId1);
|
||||
auto v2 = findOrAddVertex(vertexId2);
|
||||
v1->addNeighbor(v2);
|
||||
v2->addNeighbor(v1);
|
||||
}
|
||||
|
||||
std::shared_ptr<Vertex> Graph::findOrAddVertex(const std::string& vertexId)
|
||||
{
|
||||
auto iter = vertices_.find(vertexId);
|
||||
if (iter != vertices_.end())
|
||||
{
|
||||
return (*iter).second;
|
||||
}
|
||||
else
|
||||
{
|
||||
auto v = std::make_shared<Vertex>(vertexId);
|
||||
vertices_.insert({ vertexId, v });
|
||||
return v;
|
||||
}
|
||||
}
|
||||
85
src/LanParty.cpp
Normal file
85
src/LanParty.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
// 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/LanParty.hpp>
|
||||
|
||||
std::string LanParty::getPuzzleName() const
|
||||
{
|
||||
return "Day 23: LAN Party";
|
||||
}
|
||||
|
||||
std::string LanParty::getInputFileName() const
|
||||
{
|
||||
return "lan_party.txt";
|
||||
}
|
||||
|
||||
void LanParty::processDataLine(const std::string& line)
|
||||
{
|
||||
lan_.addEdge(line.substr(0, 2), line.substr(3, 2));
|
||||
}
|
||||
|
||||
void LanParty::finish()
|
||||
{
|
||||
auto vertices = lan_.getVertices();
|
||||
// This works as long as "ta" is present as a computername, otherwise we would have to use
|
||||
// vertices.find_if().
|
||||
auto iterTx = vertices.find(getFirstTxComputerName());
|
||||
while (iterTx != vertices.end() && iterTx->second->getId() <= getLastTxComputerName())
|
||||
{
|
||||
countInterconnectedThreeSets(*iterTx->second);
|
||||
iterTx++;
|
||||
}
|
||||
}
|
||||
|
||||
void LanParty::countInterconnectedThreeSets(const Vertex& vertexTx)
|
||||
{
|
||||
auto neighborsOfTx = vertexTx.getNeighbors();
|
||||
for (size_t i = 0; i < neighborsOfTx.size(); i++)
|
||||
{
|
||||
auto vertexA = neighborsOfTx[i].lock();
|
||||
if (shallProcessVertex(*vertexA, vertexTx))
|
||||
{
|
||||
for (size_t j{ i + 1 }; j < neighborsOfTx.size(); j++)
|
||||
{
|
||||
auto vertexB = neighborsOfTx[j].lock();
|
||||
if (shallProcessVertex(*vertexB, vertexTx))
|
||||
{
|
||||
for (auto& neighborOfA : vertexA->getNeighbors())
|
||||
{
|
||||
if (vertexB == neighborOfA.lock())
|
||||
{
|
||||
part1++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool LanParty::shallProcessVertex(const Vertex& vertexToCheck, const Vertex& vertexTx) const
|
||||
{
|
||||
return (vertexToCheck.getId()[0] != 't' || vertexToCheck.getId() > vertexTx.getId());
|
||||
}
|
||||
|
||||
const std::string LanParty::getFirstTxComputerName() const
|
||||
{
|
||||
return "ta";
|
||||
}
|
||||
|
||||
const std::string LanParty::getLastTxComputerName() const
|
||||
{
|
||||
return "tz";
|
||||
}
|
||||
@@ -28,6 +28,7 @@
|
||||
#include <aoc/PrintQueue.hpp>
|
||||
#include <aoc/GuardGallivant.hpp>
|
||||
#include <aoc/HoofIt.hpp>
|
||||
#include <aoc/LanParty.hpp>
|
||||
|
||||
void Program::run()
|
||||
{
|
||||
@@ -52,6 +53,7 @@ void Program::runSolvers()
|
||||
runSolver<PrintQueue>(solverEngine);
|
||||
runSolver<GuardGallivant>(solverEngine);
|
||||
runSolver<HoofIt>(solverEngine);
|
||||
runSolver<LanParty>(solverEngine);
|
||||
}
|
||||
|
||||
std::vector<std::string> Program::getInputPaths() const
|
||||
|
||||
38
src/Vertex.cpp
Normal file
38
src/Vertex.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
// 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/Vertex.hpp>
|
||||
|
||||
#include <memory>
|
||||
|
||||
Vertex::Vertex(const std::string id)
|
||||
: id_{ id }, neighbors_{}
|
||||
{
|
||||
}
|
||||
|
||||
std::string Vertex::getId() const
|
||||
{
|
||||
return id_;
|
||||
}
|
||||
|
||||
const std::vector<std::weak_ptr<Vertex>>& Vertex::getNeighbors() const
|
||||
{
|
||||
return neighbors_;
|
||||
}
|
||||
|
||||
void Vertex::addNeighbor(const std::shared_ptr<Vertex>& neighbor)
|
||||
{
|
||||
neighbors_.push_back(neighbor);
|
||||
}
|
||||
Reference in New Issue
Block a user