From 455b5b4c88f3558b5786d1fe2ccd5b37fc74784e Mon Sep 17 00:00:00 2001 From: PoliEcho Date: Sat, 14 Dec 2024 17:42:48 +0100 Subject: [PATCH] day6 part 1 done --- .gitignore | 4 ++ day6/day.cpp | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 day6/day.cpp diff --git a/.gitignore b/.gitignore index 722d5e7..3568359 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ +* +!*.* +!*/ .vscode +*.csv \ No newline at end of file diff --git a/day6/day.cpp b/day6/day.cpp new file mode 100644 index 0000000..3f6c2be --- /dev/null +++ b/day6/day.cpp @@ -0,0 +1,116 @@ +#include +#include +#include + +bool AreWeInBounds(int x, int y, std::vector> &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> map; + + while (std::getline(inputfile, line)) { + map.push_back(std::vector(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; +} \ No newline at end of file