advent-of-code/2024/10/main-2.cpp

54 lines
985 B
C++
Raw Permalink Normal View History

2024-12-09 21:39:44 -08:00
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string line;
vector<vector<pair<int, int>>> m;
while (getline(cin, line) && !line.empty()) {
istringstream l(line);
char c;
vector<pair<int, int>> mm;
while (l >> c) {
mm.push_back({c - '0', c == '9' ? 1 : 0});
}
m.push_back(mm);
}
int xm = m.size(), ym = m[0].size();
for (int i = 9; i > 0; i--) {
for (int x = 0; x < xm; x++) {
for (int y = 0; y < ym; y++) {
auto &[n, c] = m[x][y];
if (n != i) {
continue;
}
vector<pair<int, int>> xxyy = {
{x - 1, y}, {x + 1, y}, {x, y - 1}, {x, y + 1}};
for (auto &[xx, yy] : xxyy) {
if (xx < 0 || yy < 0 || xx >= xm || yy >= ym) {
continue;
}
auto &[nn, cc] = m[xx][yy];
if (nn == n - 1) {
cc += c;
}
}
}
}
}
int sum = 0;
for (auto &mm : m) {
for (auto &[n, c] : mm) {
if (n == 0) {
sum += c;
}
}
}
cout << sum << '\n';
}