diff --git a/include/aoc/ReindeerMaze.hpp b/include/aoc/ReindeerMaze.hpp index 1e533a4..a5f3400 100644 --- a/include/aoc/ReindeerMaze.hpp +++ b/include/aoc/ReindeerMaze.hpp @@ -36,6 +36,7 @@ class ReindeerMaze static constexpr char getEndChar(); static constexpr char getWallChar(); static constexpr int getTurnCost(); + void buildPathSegmentGraph(WeightedEdgeGraph& graph, const int entry, const int exit); void initializeWorkList(std::list& crossings, const int entryVertex); void addCheckedIncidence(std::vector& incidences, const Point2 start, const Point2 direction); diff --git a/src/ReindeerMaze.cpp b/src/ReindeerMaze.cpp index de2aeb5..ede32ba 100644 --- a/src/ReindeerMaze.cpp +++ b/src/ReindeerMaze.cpp @@ -32,11 +32,38 @@ const int ReindeerMaze::getPuzzleDay() const void ReindeerMaze::finish() { - // Initializes the graph of path segment incidences. WeightedEdgeGraph graph{}; auto entry = graph.addVertex(); auto exit = graph.addVertex(); + buildPathSegmentGraph(graph, entry, exit); + part1 = graph.dijkstra(entry, exit) / 2; +} + +constexpr char ReindeerMaze::getStartChar() +{ + return 'S'; +} + +constexpr char ReindeerMaze::getEndChar() +{ + return 'E'; +} + +constexpr char ReindeerMaze::getWallChar() +{ + return '#'; +} + +constexpr int ReindeerMaze::getTurnCost() +{ + return 1000; +} + +// Constructs the graph of path segment incidences, starting with a graph that already contains the entry and the exit +// vertices. +void ReindeerMaze::buildPathSegmentGraph(WeightedEdgeGraph& graph, const int entry, const int exit) +{ // Uses list for work items to prevent invalidation of iterators on add. std::list crossings{}; initializeWorkList(crossings, entry); @@ -56,8 +83,8 @@ void ReindeerMaze::finish() int nNext{ 0 }; while (getCharAt(position) != getEndChar() && - std::find_if(crossings.begin(), crossings.end(), - [position](auto& x) { return x.getPosition() == position; }) == crossings.end()) + std::find_if(crossings.begin(), crossings.end(), + [position](auto& x) { return x.getPosition() == position; }) == crossings.end()) { nNext = 0; Point2 nextPosition, nextDirection; @@ -143,10 +170,9 @@ void ReindeerMaze::finish() // Determines the end crossing of the new path segment and its incidence. std::list::iterator endCrossing; std::vector::iterator endIncidence; - endCrossing = nNext == 1 - ? endCrossing = std::find_if(crossings.begin(), crossings.end(), - [position](auto& x) { return x.getPosition() == position; }) - : --crossings.end(); + endCrossing = nNext == 1 ? endCrossing = std::find_if(crossings.begin(), crossings.end(), + [position](auto& x) { return x.getPosition() == position; }) + : --crossings.end(); if (endCrossing != crossings.end()) { // This incidence must exist, no need to check the incidence iterator. @@ -168,7 +194,7 @@ void ReindeerMaze::finish() } // Checks if end crossing is finished. - //checkFinishedCrossing(crossings, endCrossing); + // checkFinishedCrossing(crossings, endCrossing); if (endCrossing != crossings.end() && endCrossing->isFinished()) { crossings.erase(endCrossing); @@ -181,28 +207,6 @@ void ReindeerMaze::finish() crossings.erase(startCrossing); } } - - part1 = graph.dijkstra(entry, exit) / 2; -} - -constexpr char ReindeerMaze::getStartChar() -{ - return 'S'; -} - -constexpr char ReindeerMaze::getEndChar() -{ - return 'E'; -} - -constexpr char ReindeerMaze::getWallChar() -{ - return '#'; -} - -constexpr int ReindeerMaze::getTurnCost() -{ - return 1000; } // Initializes the work list of crossing incidences.