thanos function

int thanos(
  1. Map planet,
  2. int finger
)

Thanos-style cache eviction function - removes half of the map entries.

"Thanos can kill half lives of a world with a snap of the finger"

Eviction logic:

  • iterates through map entries in insertion order;
  • removes entries where the incremented finger counter is odd (keeps even entries);
  • guarantees exactly 50% of entries are removed (deterministic eviction).

planet is the map (cache) to "snap" (modify in-place). finger is the starting counter value (typically 0 for fresh snap).

Returns the final value of the finger counter (total number of entries processed).

Note: modifies the input map directly (in-place operation).

Implementation

int thanos(Map planet, int finger) {
  // if ++finger is odd, remove it,
  // else, let it go
  planet.removeWhere((key, value) => (++finger & 1) == 1);
  return finger;
}