Compare commits
No commits in common. "e7a7ab3cdf5581a97cced64509b083a9850bbc21" and "3ef1b59e2b0066466d72a9541fbeacd015080850" have entirely different histories.
e7a7ab3cdf
...
3ef1b59e2b
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,5 +1 @@
|
|||||||
*
|
|
||||||
!*.*
|
|
||||||
!*/
|
|
||||||
.vscode
|
.vscode
|
||||||
*.csv
|
|
||||||
116
day6/day.cpp
116
day6/day.cpp
@ -1,116 +0,0 @@
|
|||||||
#include <fstream>
|
|
||||||
#include <iostream>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
bool AreWeInBounds(int x, int y, std::vector<std::vector<char>> &map) {
|
|
||||||
if (x < 0 || y < 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (x >= map.size() || y >= map[x].size()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
int conditional_operation(int a, int b, char op) {
|
|
||||||
switch (op) {
|
|
||||||
case '+':
|
|
||||||
return a + b;
|
|
||||||
case '-':
|
|
||||||
return a - b;
|
|
||||||
default:
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
char op_x;
|
|
||||||
char op_y;
|
|
||||||
while (true) {
|
|
||||||
map[posX][posY] = 'X';
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < map.size(); i++) {
|
|
||||||
for (int j = 0; j < map[i].size(); j++) {
|
|
||||||
if (map[i][j] == 'X') {
|
|
||||||
res++;
|
|
||||||
}
|
|
||||||
std::cout << map[i][j];
|
|
||||||
}
|
|
||||||
std::cout << std::endl;
|
|
||||||
}
|
|
||||||
std::cout << "\nRes: " << res << std::endl;
|
|
||||||
}
|
|
||||||
131
day6/second.cpp
131
day6/second.cpp
@ -1,131 +0,0 @@
|
|||||||
#include <fstream>
|
|
||||||
#include <iostream>
|
|
||||||
#include <ostream>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
bool AreWeInBounds(int x, int y, std::vector<std::vector<char>> &map) {
|
|
||||||
if (x < 0 || y < 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (x >= map.size() || y >= map[x].size()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
int conditional_operation(int a, int b, char op) {
|
|
||||||
switch (op) {
|
|
||||||
case '+':
|
|
||||||
return a + b;
|
|
||||||
case '-':
|
|
||||||
return a - b;
|
|
||||||
default:
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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() {
|
|
||||||
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 < map.size(); i++) {
|
|
||||||
for (int j = 0; j < map[i].size(); j++) {
|
|
||||||
if (map[i][j] == '^' || map[i][j] == '#') {
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
if (WillHeGetStuck(posX, posY, direction, i, j, map)) {
|
|
||||||
res++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << "\nRes: " << res << std::endl;
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user