Day 6: Part 2
This commit is contained in:
parent
033b53e100
commit
e234ed2a10
@ -4,6 +4,7 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
#include <omp.h>
|
||||||
|
|
||||||
|
|
||||||
struct Rule{
|
struct Rule{
|
||||||
|
@ -4,3 +4,7 @@ project(Aufgabe_6)
|
|||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
|
||||||
add_executable(Aufgabe_6 main.cpp)
|
add_executable(Aufgabe_6 main.cpp)
|
||||||
|
find_package(OpenMP REQUIRED)
|
||||||
|
if (OpenMP_CXX_FOUND)
|
||||||
|
target_link_libraries(Aufgabe_6 OpenMP::OpenMP_CXX)
|
||||||
|
endif()
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <omp.h>
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
int x;
|
int x;
|
||||||
@ -97,7 +98,8 @@ bool hasGuardReachedObstacle(Point guardPosition, const std::vector<Point>& map)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Point> calcGuardPositions(Guard& guard, const std::vector<Point>& map, int mapSizeX, int mapSizeY) {
|
|
||||||
|
std::vector<Point> calcGuardPositions(Guard guard, const std::vector<Point>& map, int mapSizeX, int mapSizeY) {
|
||||||
std::vector<Point> guardPositions;
|
std::vector<Point> guardPositions;
|
||||||
while(guard.currentPosition.isValid(mapSizeX, mapSizeY)) {
|
while(guard.currentPosition.isValid(mapSizeX, mapSizeY)) {
|
||||||
bool duplicated = false;
|
bool duplicated = false;
|
||||||
@ -121,7 +123,90 @@ std::vector<Point> calcGuardPositions(Guard& guard, const std::vector<Point>& ma
|
|||||||
return guardPositions;
|
return guardPositions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**std::vector<Point> calcObstaclePositionForCycle(Guard initialGuard, const std::vector<Point>& map, int mapSizeX, int mapSizeY) {
|
||||||
|
Guard guard = initialGuard;
|
||||||
|
std::vector<Point> possibleObstaclePositions;
|
||||||
|
|
||||||
|
#pragma omp parallel
|
||||||
|
// Lokaler Vector für jeden Thread
|
||||||
|
std::vector<Point> localObstaclePositions;
|
||||||
|
for(int x=0; x < mapSizeX; x++) {
|
||||||
|
#pragma omp for collapse(2) schedule(dynamic)
|
||||||
|
for(int y =0; y < mapSizeY; y++) {
|
||||||
|
Guard localGuard = guard;
|
||||||
|
std::vector<Point> guardPositions;
|
||||||
|
std::vector<Point> currentMap = map;
|
||||||
|
currentMap.emplace_back(x, y);
|
||||||
|
while(localGuard.currentPosition.isValid(mapSizeX, mapSizeY)) {
|
||||||
|
guardPositions.push_back(guard.currentPosition);
|
||||||
|
|
||||||
|
if(guardPositions.size() > (mapSizeX * mapSizeY)) {
|
||||||
|
localObstaclePositions.emplace_back(x, y);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
Point nextPosition = calcUpdatedGuardPosition(localGuard.currentPosition, localGuard.currentDirection);
|
||||||
|
if(hasGuardReachedObstacle(nextPosition, currentMap)) {
|
||||||
|
localGuard.currentDirection = static_cast<Direction>((localGuard.currentDirection + 1) % 4);
|
||||||
|
} else {
|
||||||
|
localGuard.currentPosition = nextPosition;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
guard = initialGuard;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Zusammenführen der lokalen Ergebnisse in den globalen Vector
|
||||||
|
#pragma omp critical
|
||||||
|
possibleObstaclePositions.insert(possibleObstaclePositions.end(), localObstaclePositions.begin(), localObstaclePositions.end());
|
||||||
|
return possibleObstaclePositions;
|
||||||
|
}**/
|
||||||
|
|
||||||
|
std::vector<Point> calculateObstaclePositions(Guard initialGuard, std::vector<Point> map, int mapSizeX, int mapSizeY) {
|
||||||
|
Guard guard = initialGuard;
|
||||||
|
std::vector<Point> possibleObstaclePositions;
|
||||||
|
|
||||||
|
// OpenMP für Parallelisierung der äußeren Schleife
|
||||||
|
#pragma omp parallel
|
||||||
|
{
|
||||||
|
// Lokaler Vector für jeden Thread
|
||||||
|
std::vector<Point> localObstaclePositions;
|
||||||
|
|
||||||
|
#pragma omp for collapse(2) schedule(dynamic)
|
||||||
|
for (int x = 0; x < mapSizeX; x++) {
|
||||||
|
for (int y = 0; y < mapSizeY; y++) {
|
||||||
|
Guard localGuard = guard; // Kopie des Guards für jeden Thread
|
||||||
|
std::vector<Point> guardPositions;
|
||||||
|
std::vector<Point> currentMap = map; // Lokale Kopie der Karte
|
||||||
|
currentMap.emplace_back(x, y);
|
||||||
|
|
||||||
|
while (localGuard.currentPosition.isValid(mapSizeX, mapSizeY)) {
|
||||||
|
guardPositions.push_back(localGuard.currentPosition);
|
||||||
|
|
||||||
|
if (guardPositions.size() > (mapSizeX * mapSizeY)) {
|
||||||
|
localObstaclePositions.emplace_back(x, y);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
Point nextPosition = calcUpdatedGuardPosition(localGuard.currentPosition, localGuard.currentDirection);
|
||||||
|
if (hasGuardReachedObstacle(nextPosition, currentMap)) {
|
||||||
|
localGuard.currentDirection = static_cast<Direction>((localGuard.currentDirection + 1) % 4);
|
||||||
|
} else {
|
||||||
|
localGuard.currentPosition = nextPosition;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Zusammenführen der lokalen Ergebnisse in den globalen Vector
|
||||||
|
#pragma omp critical
|
||||||
|
possibleObstaclePositions.insert(possibleObstaclePositions.end(), localObstaclePositions.begin(), localObstaclePositions.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
return possibleObstaclePositions;
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
std::vector<std::string> input = readTextFromFile("../input.txt");
|
std::vector<std::string> input = readTextFromFile("../input.txt");
|
||||||
Point initialGuardPos = calcGuardPositionFromInput(input);
|
Point initialGuardPos = calcGuardPositionFromInput(input);
|
||||||
std::vector<Point> obstaclePositions = calcObstaclePositionsFromInput(input);
|
std::vector<Point> obstaclePositions = calcObstaclePositionsFromInput(input);
|
||||||
@ -129,5 +214,8 @@ int main() {
|
|||||||
auto visitedPoints = calcGuardPositions(guard, obstaclePositions, input[0].size(), input.size());
|
auto visitedPoints = calcGuardPositions(guard, obstaclePositions, input[0].size(), input.size());
|
||||||
|
|
||||||
std::cout << "The guard visited " << visitedPoints.size() << " points!" << std::endl;
|
std::cout << "The guard visited " << visitedPoints.size() << " points!" << std::endl;
|
||||||
|
|
||||||
|
auto possibleObstaclePositions = calculateObstaclePositions(guard, obstaclePositions, input[0].size(), input.size());
|
||||||
|
std::cout << "There are " << possibleObstaclePositions.size() << " possible positions!" << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user