advent-of-code/2023/5/main-2.cpp
eriedaberrie a814a54c34 2023 day 5
I did not have fun

These problems were very unenjoyable imo (skill issue probably)
2023-12-04 22:21:01 -08:00

67 lines
1.5 KiB
C++

#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin.ignore(6);
string line;
getline(cin, line);
istringstream sls(line);
vector<pair<long, long>> seeds;
vector<bool> done;
while (!sls.eof()) {
long n, nn;
sls >> n >> nn;
seeds.push_back(make_pair(n, n + nn - 1));
done.push_back(false);
}
cin.ignore(1);
for (int mapn = 0; mapn < 7; mapn++) {
cin.ignore(500, '\n');
while (getline(cin, line) && !line.empty()) {
istringstream sls(line);
long d, s, r;
sls >> d >> s >> r;
int ssize = seeds.size();
for (int i = 0; i < ssize; i++) {
if (done[i])
continue;
auto& curr = seeds[i];
long *curs = &get<0>(curr), *cure = &get<1>(curr);
if (*curs >= s) {
if (*cure < s + r) {
*curs += d - s;
*cure += d - s;
done[i] = true;
} else if (*curs < s + r) {
seeds.push_back(make_pair(*curs + d - s, d + r - 1));
done.push_back(true);
*curs = s + r;
}
} else if (*cure > s + r) {
seeds.push_back(make_pair(d, d + r - 1));
done.push_back(true);
seeds.push_back(make_pair(s + r, *cure));
done.push_back(false);
*cure = s - 1;
} else if (*cure > s) {
seeds.push_back(make_pair(d, *cure + d - s));
done.push_back(true);
*cure = s - 1;
}
}
}
fill(done.begin(), done.end(), false);
}
int min = INT_MAX;
for (auto& r : seeds) {
if (get<0>(r) < min)
min = get<0>(r);
}
cout << min << '\n';
}