Skip to main content
minor update
Source Link
Arnauld
  • 206.5k
  • 21
  • 189
  • 673
a => {                // a[] = input as [ deck0, deck1 ]
  for(                // main loop:
    d = [],           //   d[] = table deck
    n =               //   n = number of cards we're allowed to draw
    p =               //   p = current player
    T = C = 0;        //   (T, C) = output (T = tricks / C = cards)
    a[p ^= 1] + d;    //   before each iteration, change p to 'the other player'
                      //   stop when both his deck and the table deck are empty
                      //   (i.e. the current player has all the cards)
    !n | c ?          //   after each iteration, if n is zero or c is not zero:
      n = c           //     set n to c
    :                 //   else:
      --n ?           //     decrement n; if it's not zero:
        p ^= 1        //       forcemake the current player to play again
      :               //     else:
        d = a[T++, p] //       end of trick: increment T
          .push(...d) //       push the table deck into the other player's deck
          && []       //       and clear the table deck
  )                   //
    d.push(           //     at each iteration,
      c = a[p ^ 1]    //     draw the card c from the deck of the current player,
        .shift(C++)   //     increment C, and push c into d[]the table deck
    );                // end of for()
  return [T, C]       // return [ T, C ]
}                     //
a => {                // a[] = input as [ deck0, deck1 ]
  for(                // main loop:
    d = [],           //   d[] = table deck
    n =               //   n = number of cards we're allowed to draw
    p =               //   p = current player
    T = C = 0;        //   (T, C) = output (T = tricks / C = cards)
    a[p ^= 1] + d;    //   before each iteration, change p to 'the other player'
                      //   stop when both his deck and the table deck are empty
                      //   (i.e. the current player has all the cards)
    !n | c ?          //   after each iteration, if n is zero or c is not zero:
      n = c           //     set n to c
    :                 //   else:
      --n ?           //     decrement n; if it's not zero:
        p ^= 1        //       force the current player to play again
      :               //     else:
        d = a[T++, p] //       end of trick: increment T
          .push(...d) //       push the table deck into the other player's deck
          && []       //       and clear the table deck
  )                   //
    d.push(           //     at each iteration,
      c = a[p ^ 1]    //     draw the card c from the deck of the current player,
        .shift(C++)   //     increment C, and push c into d[]
    );                // end of for()
  return [T, C]       // return [ T, C ]
}                     //
a => {                // a[] = input as [ deck0, deck1 ]
  for(                // main loop:
    d = [],           //   d[] = table deck
    n =               //   n = number of cards we're allowed to draw
    p =               //   p = current player
    T = C = 0;        //   (T, C) = output (T = tricks / C = cards)
    a[p ^= 1] + d;    //   before each iteration, change p to 'the other player'
                      //   stop when both his deck and the table deck are empty
                      //   (i.e. the current player has all the cards)
    !n | c ?          //   after each iteration, if n is zero or c is not zero:
      n = c           //     set n to c
    :                 //   else:
      --n ?           //     decrement n; if it's not zero:
        p ^= 1        //       make the current player play again
      :               //     else:
        d = a[T++, p] //       end of trick: increment T
          .push(...d) //       push the table deck into the other player's deck
          && []       //       and clear the table deck
  )                   //
    d.push(           //   at each iteration,
      c = a[p ^ 1]    //   draw the card c from the deck of the current player,
        .shift(C++)   //   increment C, and push c into the table deck
    );                // end of for()
  return [T, C]       // return [ T, C ]
}                     //
minor clarification
Source Link
Arnauld
  • 206.5k
  • 21
  • 189
  • 673
a => {                // a[] = input as [ deck0, deck1 ]
  for(                // main loop:
    d = [],           //   d[] = table deck
    n =               //   n = number of cards we're allowed to draw
    p =               //   p = current player
    T = C = 0;        //   (T, C) = output (T = tricks / C = cards)
    a[p ^= 1] + d;    //   before each iteration, change p to 'the other player'
                      //   stop when both his deck and the table deck are empty
                      //   (i.e. the current player has all the cards)
    !n | c ?          //   after each iteration, if n is zero or c is not zero:
      n = c           //     set n to c
    :                 //   else:
      --n ?           //     decrement n; if it's not zero:
        p ^= 1        //       force the current player to play again
      :               //     else:
        d = a[T++, p] //       end of trick: increment T
          .push(...d) //       push the table deck into the other player's deck
          && []       //       and clear the table deck
  )                   //
    d.push(           //     at each iteration,
      c = a[p ^ 1]    //     draw the card c from the deck of the current player,
        .shift(C++)   //     increment C, and push c into d[]
    );                // end of for()
  return [T, C]       // return [ T, C ]
}                     //
a => {                // a[] = input as [ deck0, deck1 ]
  for(                // main loop:
    d = [],           //   d[] = table deck
    n =               //   n = number of cards we're allowed to draw
    p =               //   p = current player
    T = C = 0;        //   (T, C) = output (T = tricks / C = cards)
    a[p ^= 1] + d;    //   before each iteration, change p to 'the other player'
                      //   stop when both his deck and the table deck are empty
    !n | c ?          //   after each iteration, if n is zero or c is not zero:
      n = c           //     set n to c
    :                 //   else:
      --n ?           //     decrement n; if it's not zero:
        p ^= 1        //       force the current player to play again
      :               //     else:
        d = a[T++, p] //       end of trick: increment T
          .push(...d) //       push the table deck into the other player's deck
          && []       //       and clear the table deck
  )                   //
    d.push(           //     at each iteration,
      c = a[p ^ 1]    //     draw the card c from the deck of the current player,
        .shift(C++)   //     increment C, and push c into d[]
    );                // end of for()
  return [T, C]       // return [ T, C ]
}                     //
a => {                // a[] = input as [ deck0, deck1 ]
  for(                // main loop:
    d = [],           //   d[] = table deck
    n =               //   n = number of cards we're allowed to draw
    p =               //   p = current player
    T = C = 0;        //   (T, C) = output (T = tricks / C = cards)
    a[p ^= 1] + d;    //   before each iteration, change p to 'the other player'
                      //   stop when both his deck and the table deck are empty
                      //   (i.e. the current player has all the cards)
    !n | c ?          //   after each iteration, if n is zero or c is not zero:
      n = c           //     set n to c
    :                 //   else:
      --n ?           //     decrement n; if it's not zero:
        p ^= 1        //       force the current player to play again
      :               //     else:
        d = a[T++, p] //       end of trick: increment T
          .push(...d) //       push the table deck into the other player's deck
          && []       //       and clear the table deck
  )                   //
    d.push(           //     at each iteration,
      c = a[p ^ 1]    //     draw the card c from the deck of the current player,
        .shift(C++)   //     increment C, and push c into d[]
    );                // end of for()
  return [T, C]       // return [ T, C ]
}                     //
saved 2 bytes
Source Link
Arnauld
  • 206.5k
  • 21
  • 189
  • 673

JavaScript (ES6), 119117 bytes

a=>{for(d=[],n=p=T=C=0;a[p^=1]+d;d.push(c)*n=p=T=C=0;a[p^=1]+d;!n|c?n=c:--n?p^=1:d=a[T++,p].push(...d)&&[])d.push(c=a[p^1].shift(C++));return[T,C]}

Try it online!Try it online!

a => {                // a[] = input as [ deck0, deck1 ]
  for(                // main loop:
    d = [],           //   d[] = table deck
    n =               //   n = number of cards we're allowed to draw
    p =               //   p = current player
    T = C = 0;        //   (T, C) = output (T = tricks / C = cards)
    a[p ^= 1] + d;    //   before each iteration, change p to 'the other player'
                      //   stop when both his deck and the table deck are empty
    d.push(c) *       //   after each iteration, push c into d[]
    !n | c ?          //   after each iteration, if n is zero or c is not zero:
      n = c           //     set n to c
    :                 //   else:
      --n ?           //     decrement n; if it's not zero:
        p ^= 1        //       force the current player to play again
      :               //     else:
        d = a[T++, p] //       end of trick: increment T
          .push(...d) //       push the table deck into the other player's deck
          && []       //       and clear the table deck
  )                   //
    cd.push( = a[p ^ 1]       //     at each iteration,
      c = a[p ^ 1]    //     draw the card c from the deck of the current player,
        .shift(C++);    //     the currentincrement playerC, and incrementpush C
c into d[]
    );                // end of for()
  return [T, C]       // return [ T, C ]
}                     //

JavaScript (ES6), 119 bytes

a=>{for(d=[],n=p=T=C=0;a[p^=1]+d;d.push(c)*!n|c?n=c:--n?p^=1:d=a[T++,p].push(...d)&&[])c=a[p^1].shift(C++);return[T,C]}

Try it online!

a => {                // a[] = input as [ deck0, deck1 ]
  for(                // main loop:
    d = [],           //   d[] = table deck
    n =               //   n = number of cards we're allowed to draw
    p =               //   p = current player
    T = C = 0;        //   (T, C) = output (T = tricks / C = cards)
    a[p ^= 1] + d;    //   before each iteration, change p to 'the other player'
                      //   stop when both his deck and the table deck are empty
    d.push(c) *       //   after each iteration, push c into d[]
    !n | c ?          //   if n is zero or c is not zero:
      n = c           //     set n to c
    :                 //   else:
      --n ?           //     decrement n; if it's not zero:
        p ^= 1        //       force the current player to play again
      :               //     else:
        d = a[T++, p] //       end of trick: increment T
          .push(...d) //       push the table deck into the other player's deck
          && []       //       and clear the table deck
  )                   //
    c = a[p ^ 1]      //     at each iteration, draw the card c from the deck of
      .shift(C++);    //     the current player and increment C
                      // end of for()
  return [T, C]       // return [ T, C ]
}                     //

JavaScript (ES6), 117 bytes

a=>{for(d=[],n=p=T=C=0;a[p^=1]+d;!n|c?n=c:--n?p^=1:d=a[T++,p].push(...d)&&[])d.push(c=a[p^1].shift(C++));return[T,C]}

Try it online!

a => {                // a[] = input as [ deck0, deck1 ]
  for(                // main loop:
    d = [],           //   d[] = table deck
    n =               //   n = number of cards we're allowed to draw
    p =               //   p = current player
    T = C = 0;        //   (T, C) = output (T = tricks / C = cards)
    a[p ^= 1] + d;    //   before each iteration, change p to 'the other player'
                      //   stop when both his deck and the table deck are empty
    !n | c ?          //   after each iteration, if n is zero or c is not zero:
      n = c           //     set n to c
    :                 //   else:
      --n ?           //     decrement n; if it's not zero:
        p ^= 1        //       force the current player to play again
      :               //     else:
        d = a[T++, p] //       end of trick: increment T
          .push(...d) //       push the table deck into the other player's deck
          && []       //       and clear the table deck
  )                   //
    d.push(           //     at each iteration,
      c = a[p ^ 1]    //     draw the card c from the deck of the current player,
        .shift(C++)   //     increment C, and push c into d[]
    );                // end of for()
  return [T, C]       // return [ T, C ]
}                     //
added a commented version
Source Link
Arnauld
  • 206.5k
  • 21
  • 189
  • 673
Loading
Source Link
Arnauld
  • 206.5k
  • 21
  • 189
  • 673
Loading