← Back to context

Comment by LonelyWolfe

1 year ago

Lemme know if there's another way to press every combination:

let maxElementReachedForElement = {}; let totalElements = 0; let firstElement = 0; let secondElement = 0;

setInterval(function() {

  document.getElementsByClassName('mobile-item')[firstElement].getElementsByClassName('item')[0].click();

  document.getElementsByClassName('mobile-item')[secondElement].getElementsByClassName('item')[0].click();

  totalElements = document.getElementsByClassName('mobile-item').length;

  secondElement = (secondElement + 1) % totalElements;

  if (secondElement == 0) {
   maxElementReachedForElement[firstElement] = totalElements; 
   if (Object.keys(maxElementReachedForElement).some(item => maxElementReachedForElement[item] < totalElements)) {
    let prevStart = Object.keys(maxElementReachedForElement).find(item => maxElementReachedForElement[item] < totalElements);
    firstElement = prevStart;
    secondElement = maxElementReachedForElement[prevStart]; // Start from previous end
   } else {
    firstElement = (firstElement + 1) % totalElements;
    secondElement = firstElement; // No need to repeat the previous combinations.
   }
  }

  document.title = firstElement + '+' + secondElement + '|' + totalElements;

}, 500); // TODO : Find a way other than delay

I think you can reduce state. Rather than tracking maxElementReached per-element, maintain a single maxElementReached for the first n elements. March the first n elements forward in lockstep, and grow n by 1 whenever you exhaust all available combinations for that set

  1. Combine the first element with every next element until exhausted.
  2. Catch up the second element to where the first element got to.
  3. Combine the first two elements with every next element, until exhausted.
  4. Catch up the third element.
  5. Combine the first three elements with every next element
  6. etc.

In pseudocode...

  n = 1
  maxElementReached = -1
  
  while(n < totalElements()) {
    while(maxElementReached + 1 < totalElements()) {
      maxElementReached = maxElementReached + 1
      Combine each of the first n elements with element[maxElementReached]
    }

    // we've exhausted all possible combinations for the first n elements.
    // increase n by 1 and catch up the new element to keep going
    Combine element[n] with each element from n to maxElementReached
    n = n + 1
  }

minified version courtesy of GPT-4 (disclaimer I have no clue how this works)

let m={},t=0,f=0,s=0;setInterval(function(){document.getElementsByClassName('mobile-item')[f].getElementsByClassName('item')[0].click();document.getElementsByClassName('mobile-item')[s].getElementsByClassName('item')[0].click();t=document.getElementsByClassName('mobile-item').length;s=(s+1)%t;if(s==0){m[f]=t;if(Object.keys(m).some(i=>m[i]<t)){let p=Object.keys(m).find(i=>m[i]<t);f=p;s=m[p];}else{f=(f+1)%t;s=f;}}document.title=f+'+'+s+'|'+t;},500);