-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.cpp
152 lines (143 loc) · 4.24 KB
/
14.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
#include <iostream>
#include <fstream>
#include <utility>
#include <vector>
#include <algorithm>
#include <cstdio>
int part1(const std::vector<std::string>& lines) {
int m = lines.size(), n = lines[0].size();
int ans = 0;
for (int j = 0; j < n; ++j) {
int obs = -1;
int cnt = 0;
for (int i = 0; i < m; ++i) {
if (lines[i][j] == '.') continue;
if (lines[i][j] == '#') {
obs = i;
cnt = 0;
continue;
}
ans += m - (obs + cnt) - 1;
// printf("%d %d %d\n", i, j, m - (obs + cnt) - 1);
++cnt;
}
}
return ans;
}
void simulate(std::vector<std::string>& lines, std::string direction) {
int m = lines.size(), n = lines[0].size();
if (direction == "north") {
for (int j = 0; j < n; ++j) {
int obs = -1;
int cnt = 0;
for (int i = 0; i < m; ++i) {
if (lines[i][j] == '.') continue;
if (lines[i][j] == '#') {
obs = i;
cnt = 0;
continue;
}
lines[i][j] = '.';
lines[obs + cnt + 1][j] = 'O';
++cnt;
}
}
} else if (direction == "west") {
for (int i = 0; i < m; ++i) {
int obs = -1;
int cnt = 0;
for (int j = 0; j < n; ++j) {
if (lines[i][j] == '.') continue;
if (lines[i][j] == '#') {
obs = j;
cnt = 0;
continue;
}
lines[i][j] = '.';
lines[i][obs + cnt + 1] = 'O';
++cnt;
}
}
} else if (direction == "south") {
for (int j = 0; j < n; ++j) {
int obs = m;
int cnt = 0;
for (int i = m - 1; i >= 0; --i) {
if (lines[i][j] == '.') continue;
if (lines[i][j] == '#') {
obs = i;
cnt = 0;
continue;
}
lines[i][j] = '.';
lines[obs - cnt - 1][j] = 'O';
++cnt;
}
}
} else {
for (int i = 0; i < m; ++i) {
int obs = n;
int cnt = 0;
for (int j = n - 1; j >= 0; --j) {
if (lines[i][j] == '.') continue;
if (lines[i][j] == '#') {
obs = j;
cnt = 0;
continue;
}
lines[i][j] = '.';
lines[i][obs - cnt - 1] = 'O';
++cnt;
}
}
}
}
void print(const std::vector<std::string>& lines) {
for (const auto& s : lines) std::cout << s << std::endl;
std::cout<< std::endl;
}
int part2(const std::vector<std::string>& lines) {
const static std::vector<std::string> directions{"north", "west", "south", "east"};
constexpr int cycles = 1000000000;
std::vector<std::vector<std::string>> seen{lines};
int found_i = -1;
int iters = 0;
while (true) {
auto last = seen.back();
for (const auto& d : directions) {
simulate(last, d);
// printf("%s\n", d.c_str());
// print(last);
}
++iters;
for (int i = 0; i < seen.size(); ++i) {
if (seen[i] == last) {
found_i = i;
break;
}
}
if (found_i != -1) break;
seen.push_back(std::move(last));
}
// printf("%d %d %d\n", found_i, iters, seen.size());
// print(seen[found_i + (cycles - found_i) % (iters - found_i)]);
const auto& target = seen[found_i + (cycles - found_i) % (iters - found_i)];
int ans = 0;
for (int i = 0; i < target.size(); ++i) {
for (int j = 0; j < target[0].size(); ++j) {
if (target[i][j] == 'O') {
ans += target.size() - i;
}
}
}
return ans;
}
int main() {
std::ifstream f("14.txt");
std::string line;
std::vector<std::string> lines;
while (std::getline(f, line)) {
lines.push_back(std::move(line));
}
std::cout << part2(lines) << std::endl;
}