-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.cpp
139 lines (95 loc) · 3.31 KB
/
code.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
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <filesystem>
#include <string>
#include <numeric>
#include <bits/stdc++.h>
std::vector<std::vector<int>> bit;
void print_basins(std::vector<std::vector<int>> matrix);
bool is_low_point(int col, int row, std::vector<std::vector<int>> matrix) {
int col_size = matrix.size();
int row_size = matrix[0].size();
int up = ( row == 0 ) ? 10 : matrix[col][row-1];
int left = ( col == 0 ) ? 10 : matrix[col-1][row];
int down = ( row == row_size-1 ) ? 10 : matrix[col][row+1];
int right = ( col == col_size-1 ) ? 10 : matrix[col+1][row];
if ( matrix[col][row] >= up ) return false;
if ( matrix[col][row] >= left ) return false;
if ( matrix[col][row] >= down ) return false;
if ( matrix[col][row] >= right ) return false;
return true;
}
// gbs : get_basin_size
int gbs(int col, int row, std::vector<std::vector<int>> matrix) {
if ( col == -1 || row == -1 || col == matrix.size() || row == matrix[0].size() ) return 0;
if ( matrix[col][row] == 9 || bit[col][row] == 1 ) return 0;
bit[col][row] = 1;
return gbs(col,row-1,matrix) + gbs(col-1,row,matrix) + gbs(col,row+1,matrix) + gbs(col+1,row,matrix) + 1;
}
int main () {
/* Read input */
if ( !std::filesystem::exists("input") ){
std::cout << "input file does not exist" << std::endl;
return 1;
}
std::ifstream input("input");
/* Read data */
std::vector<std::vector<int>> points;
std::vector<std::pair<int,int>> low_points;
while ( !input.eof() ) {
bool is_input = true;
std::string line;
char c;
std::vector<int> row;
std::getline(input, line);
std::istringstream linestream(line);
while (linestream >> c) {
row.push_back(c-'0');
}
points.push_back(row);
}
int col_size = points.size();
int row_size = points[0].size();
/* --- Part 1 --- */
int sum = 0;
for ( int i = 0; i < col_size; i++ ) {
for ( int k = 0; k < row_size; k++ ) {
if ( is_low_point(i,k,points) ) {
std::cout << "\033[0;31m" << points[i][k] << "\033[0m ";
low_points.push_back({i,k});
sum += points[i][k] + 1;
} else {
std::cout << points[i][k] << " ";
}
}
std::cout << std::endl;
}
std::cout << "Part1: " << sum << std::endl;
/* --- Part 2 --- */
bit = std::vector(col_size, std::vector<int> (row_size, 0));
std::vector<int> basins;
for ( auto lp : low_points ) {
basins.push_back(gbs(lp.first,lp.second,points));
}
sort(basins.begin(), basins.end(), std::greater<int>());
print_basins(points);
std::cout << "Part2: " << basins[0] * basins[1] * basins[2] << std::endl;
return 0;
}
/* Pretty print because i can */
void print_basins(std::vector<std::vector<int>> matrix) {
int col_size = matrix.size();
int row_size = matrix[0].size();
for ( int i = 0; i < col_size; i++ ) {
for ( int k = 0; k < row_size; k++ ) {
if ( bit[i][k] == 1 ) {
std::cout << "\033[0;35m" << matrix[i][k] << "\033[0m ";
} else {
std::cout << "\033[0;33m" << matrix[i][k] << "\033[0m ";
}
}
std::cout << std::endl;
}
}