-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09.cpp
86 lines (75 loc) · 2.61 KB
/
09.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
#include <iostream>
#include <fstream>
#include <array>
#include <vector>
#include <queue>
#include <algorithm>
using HeightMap = std::array<std::array<int, 100>, 100>;
int charToInt(int c) {
return c - '0';
}
bool isLowest(const HeightMap& heightMap, int x, int y) {
int height = heightMap[x][y];
if (x > 0 && heightMap[x - 1][y] <= height) return false;
if (y > 0 && heightMap[x][y - 1] <= height) return false;
if (y < heightMap[0].size() - 1 && heightMap[x][y + 1] <= height) return false;
if (x < heightMap.size() - 1 && heightMap[x + 1][y] <= height) return false;
return true;
}
void partOne(const HeightMap& heightMap) {
int riskLevel = 0;
for (int i = 0; i < heightMap.size(); ++i) {
for (int j = 0; j < heightMap[i].size(); ++j) {
if (isLowest(heightMap, i, j)) riskLevel += heightMap[i][j] + 1;
}
}
std::cout << riskLevel << std::endl;
}
int searchBasin(HeightMap& heightMap, int row, int column) {
// BFS search
std::queue<std::pair<int, int>> queue;
queue.emplace(row, column);
int size = 0;
while (!queue.empty()) {
auto point = queue.front();
queue.pop();
int i = point.first;
int j = point.second;
if (heightMap[i][j] != 9) {
if (i > 0 && heightMap[i - 1][j] != 9) queue.emplace(i - 1, j);
if (j > 0 && heightMap[i][j - 1] != 9) queue.emplace(i, j - 1);
if (j < heightMap[0].size() - 1 && heightMap[i][j + 1] != 9) queue.emplace(i, j + 1);
if (i < heightMap.size() - 1 && heightMap[i + 1][j] != 9) queue.emplace(i + 1, j);
++size;
heightMap[i][j] = 9;
}
}
return size;
}
void partTwo(HeightMap& heightMap) {
std::vector<int> largestBasins;
for (int i = 0; i < heightMap.size(); ++i) {
for (int j = 0; j < heightMap[i].size(); ++j) {
if (heightMap[i][j] != 9) {
largestBasins.push_back(searchBasin(heightMap, i, j));
if (largestBasins.size() > 3)
largestBasins.erase(std::min_element(largestBasins.begin(), largestBasins.end()));
}
}
}
std::cout << largestBasins[0] * largestBasins[1] * largestBasins[2] << std::endl;
}
int main(int argc, char* argv[]) {
std::ifstream dataFile(argv[1]);
HeightMap heightMap;
for (int i = 0; i < heightMap.size(); ++i) {
for (int j = 0; j < heightMap[i].size(); ++j) {
heightMap[i][j] = charToInt(dataFile.get());
}
// get rid of newline
dataFile.get();
}
dataFile.close();
partOne(heightMap);
partTwo(heightMap);
}