// reference solution taken from https://youtu.be/7n262KKvHj8
// see also my solution, mob.c
// $ g++ -O2 -Wall -Wextra -Wpedantic -std=c++26 mob.cpp && ./a.out
//   130796	 130796	mob_cpp

#include "tictac.h"
#include <algorithm>
#include <cstring>
#include <string>

auto mob(std::string s) -> std::string {
  auto n = std::ranges::count(s, '1');
  std::string out;
  out.resize_and_overwrite(s.length(),
    [n](char *buf, size_t size) noexcept {
      std::memset(buf, '1', n - 1);
      std::memset(buf + n - 1, '0', size - n);
      buf[size - 1] = '1';
      return size;
    });
  return out;
}

int main(void) {
  static char str[100000000];
  for (size_t i = 0; i < sizeof str - 1; i++)
    str[i] = '0' + (rand() & 1);
  TICTAC(mob_cpp) mob(str);
}
