-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_09b.cpp
107 lines (96 loc) · 2.64 KB
/
day_09b.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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <queue>
#include <string>
#include <unordered_set>
#include <vector>
struct Point {
int row, col;
Point(const int row, const int col) : row(row), col (col) {}
bool operator == (const Point& p) const {
return row == p.row && col == p.col;
}
};
struct pair_hash {
std::size_t operator () (const Point& p) const {
return p.row * p.col;
}
};
void print(std::vector<std::vector<int>>& v) {
for (const auto& row : v) {
for(const auto ele : row) {
std::cout << ele;
}
std::cout << '\n';
}
}
int main(int argc, char * argv[]) {
std::string input = "../input/day_09_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::vector<std::vector<int>> h_map;
h_map.emplace_back();
while(std::getline(file, line)) {
h_map.emplace_back();
h_map.back().emplace_back(9);
for(const auto c : line) {
h_map.back().emplace_back(c - '0');
}
h_map.back().emplace_back(9);
}
const std::vector<int> temp (h_map[1].size(), 9);
h_map[0] = temp;
h_map.emplace_back(temp);
std::vector<Point> low_pts;
const auto lowest = [](const int row, const int col, const std::vector<std::vector<int>>& h_map) {
return (
h_map[row+1][col] > h_map[row][col] &&
h_map[row-1][col] > h_map[row][col] &&
h_map[row][col+1] > h_map[row][col] &&
h_map[row][col-1] > h_map[row][col]
);
};
for (int row = 1; row < h_map.size() - 1; row ++) {
for (int col = 1; col < h_map[row].size() - 1; col++) {
if (lowest(row, col, h_map)) {
low_pts.emplace_back(row, col);
}
}
}
const auto get_neighbours = [](const int row, const int col) {
return std::vector<Point> {
Point(row+1, col),
Point(row-1, col),
Point(row, col+1),
Point(row, col-1)
};
};
std::vector<long long> basin_size;
long long total_count = 1;
for (const auto& p : low_pts) {
std::queue<Point> q;
std::unordered_set<Point, pair_hash> seen;
q.push(p);
seen.insert(p);
long long count = 0;
while (!q.empty()) {
const auto neightbours = get_neighbours(q.front().row, q.front().col);
q.pop();
for (const auto& n : neightbours) {
if (h_map[n.row][n.col] != 9 && seen.find(n) == seen.end()) {
count++;
q.push(n);
seen.insert(n);
}
}
}
basin_size.emplace_back(count + 1);
}
std::sort(std::begin(basin_size), std::end(basin_size));
std::cout << basin_size[basin_size.size() - 3]*basin_size[basin_size.size() - 2]*basin_size[basin_size.size() - 1] << '\n';
return 0;
}