.consume(function (err, x, push, next) {
    if (err) {
      // pass errors along the stream and consume next value
      push(err)
      next()
    } else if (x === _.nil) {
      // pass nil (end event) along the stream
      push(null, x)
    } else {
      // first do all add, then all removes
      var things = R.groupBy(R.prop('operation'), x)

      // create all permutations of adds and removes separately
      var perms = R.mapObj(function (xs) {
        return Combinatorics.permutation(xs).toArray()
      }, things)

      // take product [adds] ++ [removes]
      var cp = Combinatorics.cartesianProduct(
        perms['add'] || [],
        perms['remove'] || []
      ).toArray()

      // for each combination, push out concatenated list again
      cp.forEach(function (c) {
        push(null, R.concat(c[0], c[1]))
      })

      next()
    }
  })
示例#2
0
文件: 21-1.js 项目: mrnz/adventofcode
  combo = function() {
    var result = Combinatorics.cartesianProduct(items.w, items.a, items.r, items.r).filter( n => {
      
      var player = new Player();

      if(n[2][0] !== 0){
        if(n[2][0] === n[3][0] ){
          return false; 
        } 
      };

      n.forEach( prep => {

        player.damage += prep[1];
        player.armor += prep[2];

      })
      return winOrLose ? simulate(player, new Boss()) : !simulate(player, new Boss());
      
      

    }).map( f => {
      return f.reduce( (sum , curr) => {  return sum + parseInt(curr[0]); },0);
    })    

    return winOrLose ? Math.min.apply(false, result) : Math.max.apply(false, result);

  }
示例#3
0
        color: 'B'
    },
    {
        name: 'S',
        color: 'B'
    }
];
const RANK = ['', 'A'].concat(_.range(2, 11)).concat('JQK'.split(''));
const RANK_RANGE = _.range(1, 14);

const deck = _.flatten(RANK_RANGE.reduce((memo, rank) => {
    return memo.concat(suits.map(suit => Object.assign({rank}, suit)));
}, []));
const deck2 = deck.slice(0);
console.log('deck: ', deck);
var possibilities = comb.cartesianProduct(deck, deck2);

let tally = new Map();
let count = 0;
possibilities.forEach(hand => {
    let value = 0;
    ++count;
    let colors = _.map(hand, 'color').join('');
    let suits = _.map(hand, 'name').join('');
    let sameRank = (hand[0].rank > 10 || hand[1].rank > 10);
    if (colors == 'RR') {
        if (sameRank) {
            value = 2;
        } else if (suits == 'HH' || suits == 'DD') {
            value = 1.66;
        } else {