-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_14b.cpp
170 lines (156 loc) · 4.69 KB
/
day_14b.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
constexpr long long iterations = 1000000000;
// Can refactor to use a single function for motion
// Or can rotate the platform and use only the north function
void tilt_east(std::vector<std::string>& platform) {
for (int col_idx = platform[0].size() - 1; col_idx >= 0; col_idx--) {
for (int row_idx = 0; row_idx < platform.size(); row_idx++) {
if (platform[row_idx][col_idx] == 'O') {
int new_col_idx = col_idx + 1;
while (new_col_idx < platform[0].size()) {
if (platform[row_idx][new_col_idx] != '.') {
new_col_idx--;
break;
}
new_col_idx++;
}
if (new_col_idx >= platform.size()) {
new_col_idx = platform.size() - 1;
}
if (col_idx != new_col_idx) {
platform[row_idx][col_idx] = '.';
platform[row_idx][new_col_idx] = 'O';
}
}
}
}
}
void tilt_west(std::vector<std::string>& platform) {
for (int col_idx = 1; col_idx < platform[0].size(); col_idx++) {
for (int row_idx = 0; row_idx < platform.size(); row_idx++) {
if (platform[row_idx][col_idx] == 'O') {
int new_col_idx = col_idx - 1;
while (new_col_idx >= 0) {
if (platform[row_idx][new_col_idx] != '.') {
new_col_idx++;
break;
}
new_col_idx--;
}
if (new_col_idx < 0) {
new_col_idx = 0;
}
if (col_idx != new_col_idx) {
platform[row_idx][col_idx] = '.';
platform[row_idx][new_col_idx] = 'O';
}
}
}
}
}
void tilt_south(std::vector<std::string>& platform) {
for (int row_idx = platform.size() - 1; row_idx >= 0; row_idx--) {
for (int col_idx = 0; col_idx < platform[0].size(); col_idx++) {
if (platform[row_idx][col_idx] == 'O') {
int new_row_idx = row_idx + 1;
while (new_row_idx < platform.size()) {
if (platform[new_row_idx][col_idx] != '.') {
new_row_idx--;
break;
}
new_row_idx++;
}
if (new_row_idx >= platform.size()) {
new_row_idx = platform.size() - 1;
}
if (row_idx != new_row_idx) {
platform[row_idx][col_idx] = '.';
platform[new_row_idx][col_idx] = 'O';
}
}
}
}
}
void tilt_north(std::vector<std::string>& platform) {
for (int row_idx = 1; row_idx < platform.size(); row_idx++) {
for (int col_idx = 0; col_idx < platform[0].size(); col_idx++) {
if (platform[row_idx][col_idx] == 'O') {
int new_row_idx = row_idx - 1;
while (new_row_idx >= 0) {
if (platform[new_row_idx][col_idx] != '.') {
new_row_idx++;
break;
}
new_row_idx--;
}
if (new_row_idx < 0) {
new_row_idx = 0;
}
if (row_idx != new_row_idx) {
platform[row_idx][col_idx] = '.';
platform[new_row_idx][col_idx] = 'O';
}
}
}
}
}
int main(int argc, char * argv[]) {
std::string input = "../input/day_14_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::vector<std::string> platform;
while(std::getline(file, line)) {
platform.push_back(line);
}
std::vector<std::vector<std::string>> cache;
const auto check_seen_before = [&cache, & platform]() {
for (int idx = 0; idx < cache.size(); idx++) {
bool equal = true;
for (int i = 0; i < platform.size() && equal; i++) {
if (platform[i] != cache[idx][i]) {
equal = false;
}
}
if (equal) return idx;
}
return -1;
};
int first_seen_at = -1;
int seen_again_at = -1; // kept for debug
bool first = true;
for (long long i = 0; i < iterations; i++) {
if (const auto idx = check_seen_before(); idx != -1) {
first_seen_at = idx;
// std::cout << "Found at " << first_seen_at << ", repeated at " << i << '\n';
seen_again_at = i;
break;
} else {
cache.push_back(platform);
}
tilt_north(platform);
tilt_west(platform);
tilt_south(platform);
tilt_east(platform);
}
// Calculate load
const int delta = seen_again_at - first_seen_at;
const int idx = (iterations - first_seen_at) % (delta) + first_seen_at;
const auto& final_platform = cache[idx];
int ans = 0;
for (int row_idx = 0; row_idx < final_platform.size(); row_idx++) {
for (int col_idx = 0; col_idx < final_platform[0].size(); col_idx++) {
ans += (final_platform[row_idx][col_idx] == 'O' ? final_platform.size() - row_idx : 0);
}
}
std::cout << ans << '\n';
return 0;
}