Skip to content

Instantly share code, notes, and snippets.

@gaburielcasado
Last active February 7, 2023 22:18
Show Gist options
  • Save gaburielcasado/6a58486c603e654697d49a48fb66b822 to your computer and use it in GitHub Desktop.
Save gaburielcasado/6a58486c603e654697d49a48fb66b822 to your computer and use it in GitHub Desktop.
hunger thingy
void main() {
var values = [4, 5, 2, 3, 1, 0];
// final values = [5, 3, 1, 2, 1];
var s = values[0];
values = values.sublist(1);
values.sort((a,b) => b.compareTo(a));
final diffs = [];
var diffSum = 0;
for (var i = 0; i < values.length - 1; i++) {
final a = values[i];
final b = values[i + 1];
final r = a - b;
diffs.add(r);
diffSum += r;
}
void printStuff() {
print(values);
print(diffs);
print('diffSum: $diffSum');
print('sammiches: $s');
print('===========');
}
for (var i = 0; i < diffs.length - 1; i++) {
printStuff();
var v1 = values[i];
var v2 = values[i + 1];
int? d0;
if (i - 1 >= 0) {
d0 = diffs[i - 1];
}
var d1 = diffs[i];
var restartFrom = i;
while (v1 > v2) {
if (s == 0) {
break;
}
d1--;
v1--;
s--;
diffSum--;
if (d0 != null) {
d0++;
diffSum++;
restartFrom = i - 2;
break;
}
}
values[i] = v1;
if (d0 != null) {
diffs[i - 1] = d0;
}
diffs[i] = d1;
i = restartFrom;
if (s == 0) {
break;
}
}
printStuff();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment