From 06b7ac26cf96a6e0a6ba5ac1247d08136f096cfc Mon Sep 17 00:00:00 2001 From: eriedaberrie Date: Thu, 5 Dec 2024 21:42:10 -0800 Subject: [PATCH] 2024 day 6 --- 2024/6/main-1.cpp | 60 ++++++++++++++++++++++++++++++++++++++ 2024/6/main-2.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 2024/6/main-1.cpp create mode 100644 2024/6/main-2.cpp diff --git a/2024/6/main-1.cpp b/2024/6/main-1.cpp new file mode 100644 index 0000000..5797b97 --- /dev/null +++ b/2024/6/main-1.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + string line; + vector lines; + while (getline(cin, line) && !line.empty()) { + istringstream l(line); + lines.push_back(line); + } + + pair dir = {-1, 0}; + pair pos; + for (int x = 0; x < lines.size(); x++) { + for (int y = 0; y < lines[x].size(); y++) { + if (lines[x][y] == '^') { + pos = {x, y}; + goto posend; + } + } + } +posend:; + + int count = 0; + while (pos.first >= 0 && pos.first < lines.size() && pos.second >= 0 && + pos.second < lines[0].size()) { + auto x = &lines[pos.first][pos.second]; + switch (*x) { + case '^': + case '.': + *x = 'X'; + count++; + break; + case '#': + pos.first -= dir.first; + pos.second -= dir.second; + switch (dir.first * 2 + dir.second) { + case -2: + dir = {0, 1}; + break; + case -1: + dir = {-1, 0}; + break; + case 1: + dir = {1, 0}; + break; + case 2: + dir = {0, -1}; + break; + } + } + pos.first += dir.first; + pos.second += dir.second; + } + + cout << count << '\n'; +} diff --git a/2024/6/main-2.cpp b/2024/6/main-2.cpp new file mode 100644 index 0000000..0c58983 --- /dev/null +++ b/2024/6/main-2.cpp @@ -0,0 +1,74 @@ +#include +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + string line; + vector lines; + while (getline(cin, line) && !line.empty()) { + istringstream l(line); + lines.push_back(line); + } + + pair pos_; + for (int x = 0; x < lines.size(); x++) { + for (int y = 0; y < lines[x].size(); y++) { + if (lines[x][y] == '^') { + pos_ = {x, y}; + goto posend; + } + } + } +posend:; + + int count = 0; + for (int x = 0; x < lines.size(); x++) { + for (int y = 0; y < lines[0].size(); y++) { + pair dir = {-1, 0}; + auto pos = pos_; + if (lines[x][y] != '.') + continue; + lines[x][y] = '#'; + + map, set> track; + while (pos.first >= 0 && pos.first < lines.size() && + pos.second >= 0 && pos.second < lines[0].size()) { + if (lines[pos.first][pos.second] == '#') { + pos.first -= dir.first; + pos.second -= dir.second; + int d = dir.first * 2 + dir.second; + if (!track.contains(pos)) { + set s; + track[pos] = s; + } else if (track[pos].contains(d)) { + count++; + goto next; + } + track[pos].insert(d); + switch (d) { + case -2: + dir = {0, 1}; + break; + case -1: + dir = {-1, 0}; + break; + case 1: + dir = {1, 0}; + break; + case 2: + dir = {0, -1}; + break; + } + } + pos.first += dir.first; + pos.second += dir.second; + } + next:; + lines[x][y] = '.'; + } + } + + cout << count << '\n'; +}