2024 day 9

This commit is contained in:
eriedaberrie 2024-12-08 22:53:26 -08:00
parent 8ef4c66148
commit 96c5f45cbc
2 changed files with 98 additions and 0 deletions

53
2024/9/main-1.cpp Normal file
View file

@ -0,0 +1,53 @@
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
char c;
deque<tuple<int, int, int>> blocks;
deque<tuple<int, int>> frees;
int i = 0, n = 0;
bool rfree = false;
while (cin >> c) {
c -= '0';
if (rfree) {
if (c > 0) {
frees.push_back({n, c});
}
} else {
blocks.push_back({i++, n, c});
}
rfree = !rfree;
n += c;
}
while (frees.size() > 0) {
auto &[bi, bx, bn] = blocks.back();
auto &[fx, fn] = frees[0];
if (fx > bx) {
break;
}
int nmoved = min(fn, bn);
bn -= nmoved;
fn -= nmoved;
blocks.push_front({bi, fx, nmoved});
if (fn == 0) {
frees.pop_front();
} else {
fx += nmoved;
}
if (bn == 0) {
blocks.pop_back();
}
}
ull sum = 0;
for (auto &[bi, bx, bn] : blocks) {
sum += (ull)bi * (bn * bx + bn * (bn - 1) / 2);
}
cout << sum << '\n';
}

45
2024/9/main-2.cpp Normal file
View file

@ -0,0 +1,45 @@
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
char c;
vector<tuple<int, int, int, bool>> blocks;
vector<tuple<int, int>> frees;
int i = 0, n = 0;
bool rfree = false;
while (cin >> c) {
c -= '0';
if (rfree) {
if (c > 0) {
frees.push_back({n, c});
}
} else {
blocks.push_back({i++, n, c, false});
}
rfree = !rfree;
n += c;
}
for (auto &[fx, fn] : frees) {
for (auto &[bi, bx, bn, bm] : blocks | views::reverse) {
if (bm || bn > fn || bx < fx) {
continue;
}
bx = fx;
bm = true;
fx += bn;
fn -= bn;
}
}
ull sum = 0;
for (auto &[bi, bx, bn, bm] : blocks) {
sum += (ull)bi * (bn * bx + bn * (bn - 1) / 2);
}
cout << sum << '\n';
}