#!/usr/bin/ruby

#
## https://rosettacode.org/wiki/Sorting_algorithms/Patience_sort
#

func patience(deck) {
  var stacks = [];
  deck.each { |card|
    given (stacks.first { card < .last }) { |stack|
      case (defined stack) {
        stack << card
      }
      default {
        stacks << [card]
      }
    }
  }

  gather {
    while (stacks) {
      take stacks.min_by { .last }.pop
      stacks.grep!{ !.is_empty }
    }
  }
}

var numbers = [7,6,5,9,8,4,3,1,2,0];
say patience(numbers);

var strs = ["John", "Kate", "Zerg", "Alice", "Joe", "Jane"];
say patience(strs);
