Add solution for "Day 23: LAN Party", part 1
This commit is contained in:
		
							parent
							
								
									e0a9a807da
								
							
						
					
					
						commit
						c783d20755
					
				
							
								
								
									
										32
									
								
								include/aoc/Graph.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								include/aoc/Graph.hpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,32 @@ | |||||||
|  | // 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 <map> | ||||||
|  | #include <memory> | ||||||
|  | #include <vector> | ||||||
|  | 
 | ||||||
|  | #include <aoc/Vertex.hpp> | ||||||
|  | 
 | ||||||
|  | class Graph | ||||||
|  | { | ||||||
|  | public: | ||||||
|  |     const std::map<std::string, std::shared_ptr<Vertex>>& getVertices() const; | ||||||
|  |     void addEdge(const std::string& vertexId1, const std::string& vertexId2); | ||||||
|  | private: | ||||||
|  |     std::map<std::string, std::shared_ptr<Vertex>> vertices_; | ||||||
|  |     std::shared_ptr<Vertex> findOrAddVertex(const std::string& vertexId); | ||||||
|  | }; | ||||||
							
								
								
									
										34
									
								
								include/aoc/LanParty.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								include/aoc/LanParty.hpp
									
									
									
									
									
										Normal file
									
								
							| @ -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 <aoc/Graph.hpp> | ||||||
|  | #include <aoc/Solver.hpp> | ||||||
|  | 
 | ||||||
|  | class LanParty : public Solver | ||||||
|  | { | ||||||
|  | public: | ||||||
|  |     std::string getPuzzleName() const override; | ||||||
|  |     std::string getInputFileName() const override; | ||||||
|  |     void processDataLine(const std::string& line) override; | ||||||
|  |     void finish() override; | ||||||
|  | private: | ||||||
|  |     Graph lan_; | ||||||
|  |     void countInterconnectedThreeSets(const Vertex& vertexTx); | ||||||
|  |     bool shallProcessVertex(const Vertex& vertexToCheck, const Vertex& vertexTx) const; | ||||||
|  |     const std::string getFirstTxComputerName() const; | ||||||
|  |     const std::string getLastTxComputerName() const; | ||||||
|  | }; | ||||||
							
								
								
									
										32
									
								
								include/aoc/Vertex.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								include/aoc/Vertex.hpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,32 @@ | |||||||
|  | // 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 <memory> | ||||||
|  | #include <string> | ||||||
|  | #include <vector> | ||||||
|  | 
 | ||||||
|  | class Vertex | ||||||
|  | { | ||||||
|  | public: | ||||||
|  |     Vertex(const std::string id); | ||||||
|  |     std::string getId() const; | ||||||
|  |     const std::vector<std::weak_ptr<Vertex>>& getNeighbors() const; | ||||||
|  |     void addNeighbor(const std::shared_ptr<Vertex>& neighbor); | ||||||
|  | private: | ||||||
|  |     std::string id_; | ||||||
|  |     std::vector<std::weak_ptr<Vertex>> neighbors_; | ||||||
|  | }; | ||||||
							
								
								
									
										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/PrintQueue.hpp> | ||||||
| #include <aoc/GuardGallivant.hpp> | #include <aoc/GuardGallivant.hpp> | ||||||
| #include <aoc/HoofIt.hpp> | #include <aoc/HoofIt.hpp> | ||||||
|  | #include <aoc/LanParty.hpp> | ||||||
| 
 | 
 | ||||||
| void Program::run() | void Program::run() | ||||||
| { | { | ||||||
| @ -52,6 +53,7 @@ void Program::runSolvers() | |||||||
|     runSolver<PrintQueue>(solverEngine); |     runSolver<PrintQueue>(solverEngine); | ||||||
|     runSolver<GuardGallivant>(solverEngine); |     runSolver<GuardGallivant>(solverEngine); | ||||||
|     runSolver<HoofIt>(solverEngine); |     runSolver<HoofIt>(solverEngine); | ||||||
|  |     runSolver<LanParty>(solverEngine); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| std::vector<std::string> Program::getInputPaths() const | 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); | ||||||
|  | } | ||||||
| @ -22,6 +22,7 @@ | |||||||
| #include <aoc/PrintQueue.hpp> | #include <aoc/PrintQueue.hpp> | ||||||
| #include <aoc/GuardGallivant.hpp> | #include <aoc/GuardGallivant.hpp> | ||||||
| #include <aoc/HoofIt.hpp> | #include <aoc/HoofIt.hpp> | ||||||
|  | #include <aoc/LanParty.hpp> | ||||||
| #include <aocTests/TestContext.hpp> | #include <aocTests/TestContext.hpp> | ||||||
| 
 | 
 | ||||||
| #define REQUIRE_MESSAGE(cond, msg) if (!(cond)) { INFO(msg); REQUIRE(cond); } | #define REQUIRE_MESSAGE(cond, msg) if (!(cond)) { INFO(msg); REQUIRE(cond); } | ||||||
| @ -116,3 +117,16 @@ TEST_CASE("[HoofItTests]") | |||||||
|         test.run(std::make_unique<HoofIt>(), 36, 81, test.getExampleInputPaths()); |         test.run(std::make_unique<HoofIt>(), 36, 81, test.getExampleInputPaths()); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | TEST_CASE("[LanPartyTests]") | ||||||
|  | { | ||||||
|  |     TestContext test; | ||||||
|  |     SECTION("FullData") | ||||||
|  |     { | ||||||
|  |         test.run(std::make_unique<LanParty>(), 1230, 0, test.getInputPaths()); | ||||||
|  |     } | ||||||
|  |     SECTION("ExampleData") | ||||||
|  |     { | ||||||
|  |         test.run(std::make_unique<LanParty>(), 7, 0, test.getExampleInputPaths()); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user