bruteforce forever

This commit is contained in:
PoliEcho 2024-12-14 21:30:37 +01:00
parent 6d94a27d97
commit f654c7452c

View File

@ -1,5 +1,6 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <ostream>
#include <vector> #include <vector>
bool AreWeInBounds(int x, int y, std::vector<std::vector<char>> &map) { bool AreWeInBounds(int x, int y, std::vector<std::vector<char>> &map) {
@ -23,80 +24,17 @@ int conditional_operation(int a, int b, char op) {
} }
} }
int main() { bool WillHeGetStuck(int posX, int posY, int direction, int locX, int locY,
std::ifstream inputfile("example"); std::vector<std::vector<char>> map) {
std::string input;
int res = 0;
if (!inputfile.is_open()) {
std::cerr << "Could not open the file" << std::endl;
return ENOENT;
}
std::string line;
std::vector<std::vector<char>> map;
while (std::getline(inputfile, line)) {
map.push_back(std::vector<char>(line.begin(), line.end()));
}
int posX_orig;
int posY_orig;
int direction;
/*
0: up
1: right
2: down
3: left
*/
for (int i = 0; i < map.size(); i++) {
for (int j = 0; j < map[i].size(); j++) {
if (map[i][j] == '^') {
posX_orig = i;
posY_orig = j;
direction = 0;
}
}
}
char op_x; char op_x;
char op_y; char op_y;
std::vector<std::vector<char>> mapTmp; map[locX][locY] = '#';
std::vector<std::vector<char>> mapRes = map; int conter = 0;
int posX;
int posY;
int counter;
for (int i = 0; i < map.size(); i++) {
for (int j = 0; j < map[i].size(); j++) {
if (map[i][j] == '^' || map[i][j] == '#') {
continue;
}
mapTmp.clear();
mapTmp = map;
mapTmp[i][j] = 'O';
posX = posX_orig;
posY = posY_orig;
counter = 0;
std::cout << "checking: " << i << " " << j << std::endl;
for (int i = 0; i < mapTmp.size(); i++) {
for (int j = 0; j < mapTmp[i].size(); j++) {
std::cout << mapTmp[i][j];
}
std::cout << "\n\n";
}
while (true) { while (true) {
if (mapTmp[posX][posY] == 'X') { map[posX][posY] = 'X';
counter++; conter++;
} else { if (conter > 10000) {
mapTmp[posX][posY] = 'X'; return true;
counter = 0;
}
if (counter > 200) {
res++;
mapRes[i][j] = 'O';
break;
} }
switch (direction) { switch (direction) {
@ -122,11 +60,11 @@ int main() {
break; break;
} }
if (!AreWeInBounds(conditional_operation(posX, 1, op_x), if (!AreWeInBounds(conditional_operation(posX, 1, op_x),
conditional_operation(posY, 1, op_y), mapTmp)) { conditional_operation(posY, 1, op_y), map)) {
break; break;
} }
switch (mapTmp[conditional_operation(posX, 1, op_x)] switch (map[conditional_operation(posX, 1, op_x)]
[conditional_operation(posY, 1, op_y)]) { [conditional_operation(posY, 1, op_y)]) {
case '#': case '#':
direction++; direction++;
@ -134,25 +72,60 @@ int main() {
direction = 0; direction = 0;
} }
break; break;
case 'O':
direction++;
if (direction > 3) {
direction = 0;
}
break;
default: default:
posX = conditional_operation(posX, 1, op_x); posX = conditional_operation(posX, 1, op_x);
posY = conditional_operation(posY, 1, op_y); posY = conditional_operation(posY, 1, op_y);
} }
} }
return false;
}
int main() {
std::ifstream inputfile("input");
std::string input;
int res = 0;
if (!inputfile.is_open()) {
std::cerr << "Could not open the file" << std::endl;
return ENOENT;
}
std::string line;
std::vector<std::vector<char>> map;
while (std::getline(inputfile, line)) {
map.push_back(std::vector<char>(line.begin(), line.end()));
}
int posX;
int posY;
int direction;
/*
0: up
1: right
2: down
3: left
*/
for (int i = 0; i < map.size(); i++) {
for (int j = 0; j < map[i].size(); j++) {
if (map[i][j] == '^') {
posX = i;
posY = j;
direction = 0;
}
} }
} }
for (int i = 0; i < mapRes.size(); i++) { for (int i = 0; i < map.size(); i++) {
for (int j = 0; j < mapRes[i].size(); j++) { for (int j = 0; j < map[i].size(); j++) {
std::cout << mapRes[i][j]; if (map[i][j] == '^' || map[i][j] == '#') {
continue;
} else {
if (WillHeGetStuck(posX, posY, direction, i, j, map)) {
res++;
} }
std::cout << std::endl;
} }
}
}
std::cout << "\nRes: " << res << std::endl; std::cout << "\nRes: " << res << std::endl;
} }