bruteforce forever
This commit is contained in:
parent
6d94a27d97
commit
f654c7452c
155
day6/second.cpp
155
day6/second.cpp
@ -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,8 +24,64 @@ int conditional_operation(int a, int b, char op) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool WillHeGetStuck(int posX, int posY, int direction, int locX, int locY,
|
||||||
|
std::vector<std::vector<char>> map) {
|
||||||
|
char op_x;
|
||||||
|
char op_y;
|
||||||
|
map[locX][locY] = '#';
|
||||||
|
int conter = 0;
|
||||||
|
while (true) {
|
||||||
|
map[posX][posY] = 'X';
|
||||||
|
conter++;
|
||||||
|
if (conter > 10000) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (direction) {
|
||||||
|
case 0:
|
||||||
|
op_x = '-';
|
||||||
|
op_y = 'n';
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
op_x = 'n';
|
||||||
|
op_y = '+';
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
op_x = '+';
|
||||||
|
op_y = 'n';
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
op_x = 'n';
|
||||||
|
op_y = '-';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
std::cerr << "How did we get here?" << std::endl;
|
||||||
|
exit(255);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!AreWeInBounds(conditional_operation(posX, 1, op_x),
|
||||||
|
conditional_operation(posY, 1, op_y), map)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (map[conditional_operation(posX, 1, op_x)]
|
||||||
|
[conditional_operation(posY, 1, op_y)]) {
|
||||||
|
case '#':
|
||||||
|
direction++;
|
||||||
|
if (direction > 3) {
|
||||||
|
direction = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
posX = conditional_operation(posX, 1, op_x);
|
||||||
|
posY = conditional_operation(posY, 1, op_y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::ifstream inputfile("example");
|
std::ifstream inputfile("input");
|
||||||
std::string input;
|
std::string input;
|
||||||
|
|
||||||
int res = 0;
|
int res = 0;
|
||||||
@ -38,8 +95,8 @@ int main() {
|
|||||||
while (std::getline(inputfile, line)) {
|
while (std::getline(inputfile, line)) {
|
||||||
map.push_back(std::vector<char>(line.begin(), line.end()));
|
map.push_back(std::vector<char>(line.begin(), line.end()));
|
||||||
}
|
}
|
||||||
int posX_orig;
|
int posX;
|
||||||
int posY_orig;
|
int posY;
|
||||||
int direction;
|
int direction;
|
||||||
/*
|
/*
|
||||||
0: up
|
0: up
|
||||||
@ -51,108 +108,24 @@ int main() {
|
|||||||
for (int i = 0; i < map.size(); i++) {
|
for (int i = 0; i < map.size(); i++) {
|
||||||
for (int j = 0; j < map[i].size(); j++) {
|
for (int j = 0; j < map[i].size(); j++) {
|
||||||
if (map[i][j] == '^') {
|
if (map[i][j] == '^') {
|
||||||
posX_orig = i;
|
posX = i;
|
||||||
posY_orig = j;
|
posY = j;
|
||||||
direction = 0;
|
direction = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
char op_x;
|
|
||||||
char op_y;
|
|
||||||
std::vector<std::vector<char>> mapTmp;
|
|
||||||
std::vector<std::vector<char>> mapRes = map;
|
|
||||||
int posX;
|
|
||||||
int posY;
|
|
||||||
int counter;
|
|
||||||
|
|
||||||
for (int i = 0; i < map.size(); i++) {
|
for (int i = 0; i < map.size(); i++) {
|
||||||
for (int j = 0; j < map[i].size(); j++) {
|
for (int j = 0; j < map[i].size(); j++) {
|
||||||
if (map[i][j] == '^' || map[i][j] == '#') {
|
if (map[i][j] == '^' || map[i][j] == '#') {
|
||||||
continue;
|
continue;
|
||||||
}
|
} else {
|
||||||
|
if (WillHeGetStuck(posX, posY, direction, i, j, map)) {
|
||||||
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) {
|
|
||||||
if (mapTmp[posX][posY] == 'X') {
|
|
||||||
counter++;
|
|
||||||
} else {
|
|
||||||
mapTmp[posX][posY] = 'X';
|
|
||||||
counter = 0;
|
|
||||||
}
|
|
||||||
if (counter > 200) {
|
|
||||||
res++;
|
res++;
|
||||||
mapRes[i][j] = 'O';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (direction) {
|
|
||||||
case 0:
|
|
||||||
op_x = '-';
|
|
||||||
op_y = 'n';
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
op_x = 'n';
|
|
||||||
op_y = '+';
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
op_x = '+';
|
|
||||||
op_y = 'n';
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
op_x = 'n';
|
|
||||||
op_y = '-';
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
std::cerr << "How did we get here?" << std::endl;
|
|
||||||
exit(255);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (!AreWeInBounds(conditional_operation(posX, 1, op_x),
|
|
||||||
conditional_operation(posY, 1, op_y), mapTmp)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (mapTmp[conditional_operation(posX, 1, op_x)]
|
|
||||||
[conditional_operation(posY, 1, op_y)]) {
|
|
||||||
case '#':
|
|
||||||
direction++;
|
|
||||||
if (direction > 3) {
|
|
||||||
direction = 0;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'O':
|
|
||||||
direction++;
|
|
||||||
if (direction > 3) {
|
|
||||||
direction = 0;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
posX = conditional_operation(posX, 1, op_x);
|
|
||||||
posY = conditional_operation(posY, 1, op_y);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < mapRes.size(); i++) {
|
|
||||||
for (int j = 0; j < mapRes[i].size(); j++) {
|
|
||||||
std::cout << mapRes[i][j];
|
|
||||||
}
|
|
||||||
std::cout << std::endl;
|
|
||||||
}
|
|
||||||
std::cout << "\nRes: " << res << std::endl;
|
std::cout << "\nRes: " << res << std::endl;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user