advent-of-code/2023/14/main-2.cpp
2023-12-16 16:52:32 -08:00

102 lines
2.1 KiB
C++

#include <bits/stdc++.h>
using namespace std;
vector<string> lines;
const int numtimes = 1000000000;
inline vector<vector<unsigned int>> hashlines() {
vector<vector<unsigned int>> ret;
for (auto& row : lines) {
vector<unsigned int> 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<vector<vector<unsigned int>>> 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';
}