-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_nine.cpp
278 lines (263 loc) · 10.5 KB
/
day_nine.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//
// Created by tei on 12/9/21.
//
#include <bits/stdc++.h>
#define line_break() { \
std::cin.ignore(10000, '\n'); \
if(strlen(buffer) == 0){ \
lastbuffernull = true; \
}\
}
enum EdgeType{
leftedge = 0,
rightedge = 1,
topedge = 2,
bottomedge = 3,
corner_tl = 6,
corner_tr = 7,
corner_bl = 8,
corner_br = 9,
notedge = 10,
unset = 11
};
const uint_fast16_t max_input_len = 2000;
typedef std::array<std::vector<uint_fast8_t>, max_input_len> Grid;
typedef std::tuple<uint_fast16_t ,uint_fast16_t> Coord;
typedef std::map<Coord,EdgeType> EdgeTMap;
inline void coord_compare_insert(std::set<Coord>& visited, std::vector<Coord>& next, uint_fast8_t thisval, uint_fast8_t comp, Coord comp_coord) {
if(thisval < comp && comp != 9 && !visited.contains(comp_coord)) {
next.push_back(comp_coord);
}
}
uint_fast32_t find_basins(Coord current, std::set<Coord>& visited, Grid& grid, EdgeTMap& edge_map, uint_fast32_t sum){
uint_fast16_t col = std::get<0>(current);
uint_fast16_t row = std::get<1>(current);
uint_fast8_t thisv = grid[col][row];
uint_fast32_t this_sum = sum;
if(!visited.contains(current)) {
this_sum++;
visited.insert(current);
}
std::vector<Coord> next;
EdgeType edge_type = edge_map[current];
switch(edge_type) {
case leftedge : {
uint_fast8_t down = grid[col][row+1];
std::tuple<uint_fast16_t,uint_fast16_t> down_coord{col,row+1};
uint_fast8_t up = grid[col][row-1];
std::tuple<uint_fast16_t,uint_fast16_t> up_coord{col,row-1};
uint_fast16_t right = grid[col+1][row];
std::tuple<uint_fast16_t ,uint_fast16_t> right_coord{col+1,row};
coord_compare_insert(visited, next,thisv,down,down_coord);
coord_compare_insert(visited, next, thisv,up,up_coord);
coord_compare_insert(visited, next, thisv,right,right_coord);
break;
}
case rightedge : {
uint_fast8_t down = grid[col][row+1];
std::tuple<uint_fast16_t ,uint_fast16_t> down_coord{col,row+1};
uint_fast8_t up = grid[col][row-1];
std::tuple<uint_fast16_t ,uint_fast16_t> up_coord{col,row-1};
uint_fast8_t left = grid[col-1][row];
std::tuple<uint_fast16_t ,uint_fast16_t> left_coord{col-1,row};
coord_compare_insert(visited, next,thisv,down,down_coord);
coord_compare_insert(visited, next, thisv,up,up_coord);
coord_compare_insert(visited, next, thisv,left,left_coord);
break;
}
case topedge : {
uint_fast8_t down = grid[col][row+1];
std::tuple<uint_fast16_t,uint_fast16_t> down_coord{col,row+1};
uint_fast8_t left = grid[col-1][row];
std::tuple<uint_fast16_t,uint_fast16_t> left_coord{col-1,row};
uint_fast8_t right = grid[col+1][row];
std::tuple<uint_fast16_t,uint_fast16_t> right_coord{col+1,row};
coord_compare_insert(visited, next,thisv,down,down_coord);
coord_compare_insert(visited, next, thisv,left,left_coord);
coord_compare_insert(visited, next, thisv,right,right_coord);
break;
}
case bottomedge : {
uint_fast8_t up = grid[col][row-1];
std::tuple<uint_fast16_t,uint_fast16_t> up_coord{col,row-1};
uint_fast8_t left = grid[col-1][row];
std::tuple<uint_fast16_t,uint_fast16_t> left_coord{col-1,row};
uint_fast8_t right = grid[col+1][row];
std::tuple<uint_fast16_t,uint_fast16_t> right_coord{col+1,row};
coord_compare_insert(visited, next, thisv,up,up_coord);
coord_compare_insert(visited, next, thisv,left,left_coord);
coord_compare_insert(visited, next, thisv,right,right_coord);
break;
}
case corner_tl : {
uint_fast8_t down = grid[col][row+1];
std::tuple<uint_fast16_t,uint_fast16_t> down_coord{col,row+1};
uint_fast8_t right = grid[col+1][row];
std::tuple<uint_fast16_t,uint_fast16_t> right_coord{col+1,row};
coord_compare_insert(visited, next,thisv,down,down_coord);
coord_compare_insert(visited, next, thisv,right,right_coord);
break;
}
case corner_tr : {
uint_fast8_t down = grid[col][row+1];
std::tuple<uint_fast16_t,uint_fast16_t> down_coord{col,row+1};
uint_fast8_t left = grid[col-1][row];
std::tuple<uint_fast16_t,uint_fast16_t> left_coord{col-1,row};
coord_compare_insert(visited, next, thisv,down,down_coord);
coord_compare_insert(visited, next, thisv,left,left_coord);
break;
}
case corner_bl : {
uint_fast8_t up = grid[col][row-1];
std::tuple<uint_fast16_t,uint_fast16_t> up_coord{col,row-1};
uint_fast8_t right = grid[col+1][row];
std::tuple<uint_fast16_t,uint_fast16_t> right_coord{col+1,row};
coord_compare_insert(visited, next, thisv,up,up_coord);
coord_compare_insert(visited, next, thisv,right,right_coord);
break;
}
case corner_br : {
uint_fast8_t up = grid[col][row-1];
std::tuple<uint_fast16_t,uint_fast16_t> up_coord{col,row-1};
uint_fast8_t left = grid[col-1][row];
std::tuple<uint_fast16_t,uint_fast16_t> left_coord{col-1,row};
coord_compare_insert(visited, next, thisv,up,up_coord);
coord_compare_insert(visited, next, thisv,left,left_coord);
break;
}
case notedge : {
uint_fast8_t up = grid[col][row-1];
std::tuple<uint_fast16_t,uint_fast16_t> up_coord{col,row-1};
uint_fast8_t down = grid[col][row+1];
std::tuple<uint_fast16_t,uint_fast16_t> down_coord{col,row+1};
uint_fast8_t left = grid[col-1][row];
std::tuple<uint_fast16_t,uint_fast16_t> left_coord{col-1,row};
uint_fast8_t right = grid[col+1][row];
std::tuple<uint_fast16_t,uint_fast16_t> right_coord{col+1,row};
coord_compare_insert(visited, next, thisv,up,up_coord);
coord_compare_insert(visited, next, thisv,down,down_coord);
coord_compare_insert(visited, next, thisv,left,left_coord);
coord_compare_insert(visited, next, thisv,right,right_coord);
break;
}
default:;
}
for(Coord c : next){
this_sum = find_basins(c, visited, grid, edge_map, this_sum);
}
return this_sum;
}
int main()
{
bool lastbuffernull = false;
uint_fast32_t low_sum = 0;
Grid grid;
uint_fast16_t curr_row = 0;
uint_fast16_t collength = 0;
while(!lastbuffernull){
char buffer[max_input_len];
std::cin.get(buffer, max_input_len );
if(strlen(buffer) != 0) {
if(curr_row == 0) {
collength = strlen(buffer);
}
for(uint_fast16_t i = 0; i < collength; ++i) {
uint_fast8_t thisint = (uint_fast8_t)buffer[i] - 48;
grid[i].push_back(thisint);
}
}
curr_row++;
line_break();
}
const uint_fast16_t rowlength = grid[0].size();
int num_matches = 0;
EdgeTMap edge_types;
std::vector<Coord> low_pt_coords;
for (uint_fast16_t r = 0; r < rowlength; r++) {
for(uint_fast16_t c = 0; c < collength; c++) {
uint_fast8_t thisint = grid[c][r];
bool leftright_low = false;
bool updown_low = false;
bool notedge_h = false;
bool notedge_v = false;
Coord this_coord{c,r};
EdgeType edge_type = unset;
if(c == 0) {
if(r == 0){
edge_type = corner_tl;
} else if(r == rowlength-1) {
edge_type = corner_bl;
} else {
edge_type = leftedge;
}
uint_fast8_t right = grid[c+1][r];
if(thisint < right) {
leftright_low = true;
}
} else if(c == collength-1) {
if(r == 0){
edge_type = corner_tr;
} else if(r == rowlength-1) {
edge_type = corner_br;
} else {
edge_type = rightedge;
}
uint_fast8_t left = grid[c-1][r];
if(thisint < left) {
leftright_low = true;
}
} else {
notedge_h = true;
uint_fast8_t left = grid[c-1][r];
uint_fast8_t right = grid[c+1][r];
if(thisint < right && thisint < left) {
leftright_low = true;
}
}
if(r == 0) {
if(edge_type == unset){
edge_type = topedge;
}
uint_fast8_t down = grid[c][r+1];
if(thisint < down) {
updown_low = true;
}
} else if(r == rowlength-1) {
if(edge_type == unset){
edge_type = bottomedge;
}
uint_fast8_t up = grid[c][r-1];
if(thisint < up) {
updown_low = true;
}
} else {
notedge_v = true;
uint_fast8_t up = grid[c][r-1];
uint_fast8_t down = grid[c][r+1];
if(thisint < up && thisint < down) {
updown_low = true;
}
}
if(notedge_h && notedge_v) {
edge_type = notedge;
}
edge_types[this_coord] = edge_type;
if(updown_low && leftright_low) {
low_sum += thisint + 1;
low_pt_coords.push_back(this_coord);
num_matches++;
}
}
}
std::vector<uint_fast32_t> basin_sums;
for(auto crd : low_pt_coords) {
std::set<Coord> visited;
uint_fast32_t this_basin_sum = find_basins(crd, visited, grid, edge_types, 0);
basin_sums.push_back(this_basin_sum);
}
std::sort(basin_sums.begin(),basin_sums.end(),[](uint_fast32_t a, uint_fast32_t b){ return a > b; });
std::cout << "num matches: " << num_matches << '\n';
std::cout << "sum of risk levels: " << low_sum << '\n';
std::cout << "product of the 3 largest basin sizes: " << basin_sums[0]*basin_sums[1]*basin_sums[2] << '\n';
return 0;
}