-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.cpp
170 lines (142 loc) · 4.23 KB
/
day14.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 <bits/stdc++.h>
#define ll long long
using namespace std;
ll nb_cycles = 1000000000;
vector<string> inp;
void parse_input() {
FILE *fp=freopen("input14.txt","r",stdin);
string line;
while (getline(cin, line)) {
inp.push_back(line);
}
fclose(fp);
}
void clear(queue<int> &q ){
std::queue<int> empty;
std::swap(q, empty);
}
vector<string> create_new_map(vector<string> input) {
vector<string> new_map;
int n = input.size(), m = input[0].size();
new_map.push_back(input[0]);
vector<queue<int>> northernmost(m);
for (int j = 0; j < m; j++) {
if (input[0][j] == '.') northernmost[j].push(0);
}
for (int i = 1; i < n; i++) {
string new_line = input[i];
for (int j = 0; j < m; j++) {
if (new_line[j] == '.') {
northernmost[j].push(i);
}
else if (new_line[j] == '#') {
clear(northernmost[j]);
}
else {
if (!northernmost[j].empty()) {
int x = northernmost[j].front(); northernmost[j].pop();
new_line[j] = '.';
new_map[x][j] = 'O';
northernmost[j].push(i);
}
}
}
new_map.push_back(new_line);
}
return new_map;
}
vector<string> rotate_south(vector<string> v) {
vector<string> ans;
int n = v.size();
for (int i = n-1; i >= 0; i--)
ans.push_back(v[i]);
return ans;
}
vector<string> south(vector<string> v) {
vector<string> new_v = rotate_south(v);
vector<string> new_map_v = create_new_map(new_v);
return rotate_south(new_map_v);
}
vector<string> rotate_west(vector<string> v) {
int n = v.size(), m = v[0].size();
string s(n, '.');
vector<string> new_v(m, s);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int new_i = j;
int new_j = n-1-i;
new_v[new_i][new_j] = v[i][j];
}
}
return new_v;
}
vector<string> west(vector<string> v) {
vector<string> new_v = rotate_west(v);
vector<string> new_map_v = create_new_map(new_v);
return rotate_west(rotate_west(rotate_west(new_map_v)));
}
vector<string> rotate_east(vector<string> v) {
return rotate_west(rotate_west(rotate_west(v)));
}
vector<string> east(vector<string> v) {
vector<string> new_v = rotate_east(v);
vector<string> new_map_v = create_new_map(new_v);
return rotate_west(new_map_v);
}
vector<string> cycle(vector<string> input) {
return east(south(west(create_new_map(input))));
}
ll solve(vector<string>& map) {
ll ans = 0;
int n = map.size(), m = map[0].size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (map[i][j] == 'O')
ans += n-i;
}
}
return ans;
}
struct CompareVectors {
bool operator()(const std::vector<std::string>& lhs, const std::vector<std::string>& rhs) const {
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
};
struct VectorWithIndex {
vector<string> vec;
ll index;
bool operator<(const VectorWithIndex& other) const {
return std::lexicographical_compare(vec.begin(), vec.end(), other.vec.begin(), other.vec.end());
}
};
ll solve2() {
set<VectorWithIndex> set_cycles;
VectorWithIndex v{inp, 0};
set_cycles.insert(v);
ll i = 1;
vector<string> cur_cycle = cycle(inp);
VectorWithIndex cur_cycle_ind{cur_cycle, i};
while (set_cycles.find(cur_cycle_ind) == set_cycles.end()) {
set_cycles.insert(cur_cycle_ind);
i++;
cur_cycle = cycle(cur_cycle);
cur_cycle_ind = VectorWithIndex{cur_cycle, i};
}
VectorWithIndex old_matched = *set_cycles.find(cur_cycle_ind);
nb_cycles -= old_matched.index;
ll len_cycle = cur_cycle_ind.index - old_matched.index;
nb_cycles = nb_cycles % len_cycle;
for (int k = 0; k < nb_cycles; k++) {
cur_cycle = cycle(cur_cycle);
}
ll ans = solve(cur_cycle);
return ans;
}
int main() {
parse_input();
vector<string> new_map = create_new_map(inp);
ll res1 = solve(new_map);
cout << res1 << endl;
ll res2 = solve2();
cout << res2 << endl;
}