2024 day 10

This commit is contained in:
eriedaberrie 2024-12-09 21:39:44 -08:00
parent 96c5f45cbc
commit 23cc0ddc1f
2 changed files with 112 additions and 0 deletions

59
2024/10/main-1.cpp Normal file
View file

@ -0,0 +1,59 @@
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string line;
vector<vector<pair<int, set<int>>>> m;
int h = 0;
while (getline(cin, line) && !line.empty()) {
istringstream l(line);
char c;
vector<pair<int, set<int>>> mm;
while (l >> c) {
set<int> s;
if (c == '9') {
s.insert(h);
}
mm.push_back({c - '0', s});
h++;
}
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.insert(c.begin(), c.end());
}
}
}
}
}
int sum = 0;
for (auto &mm : m) {
for (auto &[n, c] : mm) {
if (n == 0) {
sum += c.size();
}
}
}
cout << sum << '\n';
}

53
2024/10/main-2.cpp Normal file
View file

@ -0,0 +1,53 @@
#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';
}