-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.cpp
195 lines (158 loc) · 4.43 KB
/
solution.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
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <climits>
#include "aoc2021.h"
struct Point {
int x,y;
};
using namespace std;
void part_one(vector<vector<int>> &height_map) {
int total_risk = 0;
int rows = height_map.size();
int cols = height_map[0].size();
for (int j=0; j<rows; j++)
for (int i=0; i<cols; i++) {
bool low = true;
int current = height_map[j][i];
// check top
if (j > 0 && height_map[j-1][i] <= current)
low = false;
// check right
if (i < cols-1 && height_map[j][i+1] <= current)
low = false;
// check bottom
if (j < rows-1 && height_map[j+1][i] <= current)
low = false;
// check left
if (i > 0 && height_map[j][i-1] <= current)
low = false;
if (low)
total_risk += current+1;
}
cout << "total risk: " << total_risk << endl;
}
void draw_basin(vector<vector<int>> &height_map) {
int rows = height_map.size();
int cols = height_map[0].size();
int w = 1024 / cols;
int h = 1024 / rows;
clear();
for (int j=0; j<rows; j++)
for (int i=0; i<cols; i++) {
int color;
switch (height_map[j][i]) {
case 9: color = 0x0D2A40; break;
case 10: color = 0xBDA773; break;
default: color = 0x4F5C63; break;
}
int x0 = i * w;
int x1 = x0 + w;
int y0 = j * h;
int y1 = y0 + h;
drawrect(x0, y0, x1, y1, color);
}
nextframe();
}
int fill_basin(vector<vector<int>> &height_map, int x, int y) {
int count = 0;
int rows = height_map.size();
int cols = height_map[0].size();
queue<Point> frontier;
frontier.push({x,y});
++count;
height_map[y][x] = 10;
int frame = 0;
while (!frontier.empty()) {
Point p = frontier.front();
frontier.pop();
int i = p.x;
int j = p.y;
bool new_point = false;
// check top
if (j > 0 && height_map[j-1][i] < 9) {
frontier.push({i,j-1});
++count;
height_map[j-1][i] = 10;
new_point = true;
}
// check right
if (i < cols-1 && height_map[j][i+1] < 9) {
frontier.push({i+1,j});
++count;
height_map[j][i+1] = 10;
new_point = true;
}
// check bottom
if (j < rows-1 && height_map[j+1][i] < 9) {
frontier.push({i,j+1});
++count;
height_map[j+1][i] = 10;
new_point = true;
}
// check left
if (i > 0 && height_map[j][i-1] < 9) {
frontier.push({i-1,j});
++count;
height_map[j][i-1] = 10;
new_point = true;
}
if (new_point && (frame % 4) == 0)
draw_basin(height_map);
++frame;
}
return count;
}
void part_two(vector<vector<int>> &height_map) {
setupgif(2, "fill_basins.gif");
draw_basin(height_map);
int rows = height_map.size();
int cols = height_map[0].size();
int max_1 = INT_MIN;
int max_2 = INT_MIN;
int max_3 = INT_MIN;
fill_basins:
// loop through map to find an "unfilled" point
for (int j=0; j<rows; j++)
for (int i=0; i<cols; i++) {
if (height_map[j][i] < 9) {
int count = fill_basin(height_map, i, j);
if (count > max_1) {
max_3 = max_2;
max_2 = max_1;
max_1 = count;
} else if (count > max_2) {
max_3 = max_2;
max_2 = count;
} else if (count > max_3) {
max_3 = count;
}
goto fill_basins;
}
}
endgif();
unsigned long long result = max_1 * max_2 * max_3;
printf("Largest three basins are %d %d %d\nTheir product is %lu\n", max_1, max_2, max_3, result);
}
int main(int argc, char **argv) {
vector<vector<int>> height_map;
char c;
int i = 0;
height_map.push_back(vector<int>());
while(cin >> c) {
height_map[i].push_back(c - '0');
if (cin.peek() == '\n') {
cin.ignore();
if (cin.peek() == EOF) break;
height_map.push_back(vector<int>());
i++;
}
}
if (argc == 2)
part_two(height_map);
else
part_one(height_map);
return 0;
}