From f464bbfb26c0e7b494d1b33659c84424740fa540 Mon Sep 17 00:00:00 2001 From: eriedaberrie Date: Sat, 16 Dec 2023 16:52:32 -0800 Subject: [PATCH] 2023 day 14 --- 2023/14/main-1.cpp | 31 ++++++++++++++ 2023/14/main-2.cpp | 101 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 2023/14/main-1.cpp create mode 100644 2023/14/main-2.cpp diff --git a/2023/14/main-1.cpp b/2023/14/main-1.cpp new file mode 100644 index 0000000..e1cbb48 --- /dev/null +++ b/2023/14/main-1.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + vector lines; + + string line; + while (getline(cin, line) && !line.empty()) { + lines.push_back(line); + } + + int ret = 0; + for (unsigned int r = 0; r < lines.size(); r++) { + for (unsigned int c = 0; c < lines[r].size(); c++) { + if (lines[r][c] == 'O') { + lines[r][c] = '.'; + int rr = r; + while (rr != 0 && lines[rr - 1][c] == '.') { + rr--; + } + lines[rr][c] = 'O'; + ret += lines.size() - rr; + } + } + } + + cout << ret << '\n'; +} diff --git a/2023/14/main-2.cpp b/2023/14/main-2.cpp new file mode 100644 index 0000000..b99766d --- /dev/null +++ b/2023/14/main-2.cpp @@ -0,0 +1,101 @@ +#include +using namespace std; + +vector lines; + +const int numtimes = 1000000000; + +inline vector> hashlines() { + vector> ret; + for (auto& row : lines) { + vector retl; + for (unsigned int c = 0; c < row.size(); c++) { + if (row[c] == 'O') { + retl.push_back(c); + } + } + ret.push_back(retl); + } + return ret; +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + string line; + while (getline(cin, line) && !line.empty()) { + lines.push_back(line); + } + + vector>> all; + + for (unsigned int i = 0; i < numtimes; i++) { + for (unsigned int r = 0; r < lines.size(); r++) { + for (unsigned int c = 0; c < lines[r].size(); c++) { + if (lines[r][c] == 'O') { + lines[r][c] = '.'; + unsigned int rr = r; + while (rr != 0 && lines[rr - 1][c] == '.') { + rr--; + } + lines[rr][c] = 'O'; + } + } + } + for (auto& row : lines) { + for (unsigned int c = 0; c < row.size(); c++) { + if (row[c] == 'O') { + row[c] = '.'; + unsigned int cc = c; + while (cc != 0 && row[cc - 1] == '.') { + cc--; + } + row[cc] = 'O'; + } + } + } + for (unsigned int r = lines.size() - 1; (int)r >= 0; r--) { + for (unsigned int c = 0; c < lines[r].size(); c++) { + if (lines[r][c] == 'O') { + lines[r][c] = '.'; + unsigned int rr = r; + while (rr != lines.size() - 1 && lines[rr + 1][c] == '.') { + rr++; + } + lines[rr][c] = 'O'; + } + } + } + for (auto& row : lines) { + for (unsigned int c = row.size() - 1; (int)c >= 0; c--) { + if (row[c] == 'O') { + row[c] = '.'; + unsigned int cc = c; + while (cc != row.size() - 1 && row[cc + 1] == '.') { + cc++; + } + row[cc] = 'O'; + } + } + } + const auto& hashed = hashlines(); + for (unsigned int j = 0; j < i; j++) { + if (all[j] == hashed) { + unsigned int diff = i - j; + all.push_back(all[(numtimes - j - 1) % diff + j]); + goto mainexit; + } + } + all.push_back(hashed); + } + mainexit: + + auto& lastver = all.back(); + int ret = 0; + for (unsigned int r = 0; r < lastver.size(); r++) { + ret += (lastver.size() - r) * lastver[r].size(); + } + + cout << ret << '\n'; +}