Skip to content

Instantly share code, notes, and snippets.

@mizuhara
Last active August 29, 2015 13:55
Show Gist options
  • Save mizuhara/8752226 to your computer and use it in GitHub Desktop.
Save mizuhara/8752226 to your computer and use it in GitHub Desktop.
var _ = require('underscore');
function sum(ary) {
'use strict';
return _.reduce(ary, function(acc, e) {
return _.isEmpty(e) ? acc + 0 : e === 'x' ? acc + 1 : acc + parseInt(e, 10);
}, 0);
}
function chooseCash(cash) {
'use strict';
return _.chain( _.zip(cash, [0, 1, 2, 3, 4]) )
.sortBy(function(e) { return sum( e[0].split(',') ); })
.map(function(e) { return e[1]; } )
.first()
.value();
}
function process(cash) {
'use strict';
var performance = [2, 7, 3, 5, 2],
index = 0,
persons = 0;
return _.map(_.zip(cash, [0, 1, 2, 3, 4]), function(e) {
index = _.indexOf(_.toArray(e[0]), 'x');
if(index === -1) {
persons = sum( e[0].split(',') );
persons -= _.min([persons, performance[e[1]]]);
return persons.toString();
} else {
persons = sum( e[0].substr(0, index).split(',') );
persons -= _.min([persons, performance[e[1]]]);
return [persons, e[0].substr(index)].join();
}
});
}
function solve(src) {
'use strict';
var cash = ['0', '0', '0', '0', '0'],
cashNo = 0;
_.each(_.toArray(src), function(e) {
if(e !== '.') {
cashNo = chooseCash(cash);
cash[cashNo] = [cash[cashNo], e].join();
} else {
cash = process(_.clone(cash));
}
});
return _.map(cash, function(e) {
return sum( e.split(',') );
}).join();
}
module.exports = {
solve: solve
};
var yhpg = require('./yhpg');
describe("yhpg.solve", function() {
var inputs = [
/*0*/ ['42873x.3.', '0,4,2,0,0'],
/*1*/ ['1', '1,0,0,0,0'],
/*2*/ ['.', '0,0,0,0,0'],
/*3*/ ['x', '1,0,0,0,0'],
/*4*/ ['31.', '1,0,0,0,0'],
/*5*/ ['3x.', '1,1,0,0,0'],
/*6*/ ['99569x', '9,9,6,6,9'],
/*7*/ ['99569x33', '9,9,9,9,9'],
/*8*/ ['99569x33.', '7,2,6,4,7'],
/*9*/ ['99569x33..', '5,0,4,0,5'],
/*10*/ ['12345x3333.', '4,0,3,2,3'],
/*11*/ ['54321x3333.', '3,0,3,0,4'],
/*12*/ ['51423x3333.', '3,4,4,0,4'],
/*13*/ ['12x34x.', '1,0,1,0,2'],
/*14*/ ['987x654x.32', '7,6,4,10,5'],
/*15*/ ['99999999999x99999999.......9.', '20,10,12,5,20'],
/*16*/ ['997', '9,9,7,0,0'],
/*17*/ ['.3.9', '1,9,0,0,0'],
/*18*/ ['832.6', '6,6,0,0,0'],
/*19*/ ['.5.568', '3,5,6,8,0'],
/*20*/ ['475..48', '4,8,0,0,0'],
/*21*/ ['7.2..469', '1,4,6,9,0'],
/*22*/ ['574x315.3', '3,3,1,7,1'],
/*23*/ ['5.2893.x98', '10,9,5,4,1'],
/*24*/ ['279.6xxx..4', '2,1,4,1,1'],
/*25*/ ['1.1.39..93.x', '7,1,0,0,0'],
/*26*/ ['7677749325927', '16,12,17,18,12'],
/*27*/ ['x6235.87.56.9.', '7,2,0,0,0'],
/*28*/ ['4.1168.6.197.6.', '0,0,3,0,0'],
/*29*/ ['2.8.547.25..19.6', '6,2,0,0,0'],
/*30*/ ['.5.3x82x32.1829..', '5,0,5,0,7'],
/*31*/ ['x.1816..36.24.429.', '1,0,0,0,7'],
/*32*/ ['79.2.6.81x..26x31.1', '1,0,2,1,1'],
/*33*/ ['574296x6538984..5974', '14,13,10,15,14'],
/*34*/ ['99.6244.4376636..72.6', '5,6,0,0,3'],
/*35*/ ['1659.486x5637168278123', '17,16,16,18,17'],
/*36*/ ['.5.17797.x626x5x9457.3.', '14,0,3,5,8'],
/*37*/ ['..58624.85623..4.7..23.x', '1,1,0,0,0'],
/*38*/ ['716.463.9.x.8..4.15.738x4', '7,3,5,8,1'],
/*39*/ ['22xx.191.96469472.7232377.', '10,11,18,12,9'],
/*40*/ ['24..4...343......4.41.6...2', '2,0,0,0,0'],
/*41*/ ['32732.474x153.866..4x29.2573', '7,5,7,8,5'],
/*42*/ ['786.1267x9937.17.15448.1x33.4', '4,4,8,4,10'],
/*43*/ ['671714849.149.686852.178.895x3', '13,16,13,10,12'],
/*44*/ ['86x.47.517..29621.61x937..xx935', '7,11,8,8,10'],
/*45*/ ['.2233.78x.94.x59511.5.86x3.x714.', '4,6,10,8,8'],
/*46*/ ['.793...218.687x415x13.1...x58576x', '8,11,8,6,9'],
/*47*/ ['6.6x37.3x51x932.72x4x33.9363.x7761', '15,13,15,12,15'],
/*48*/ ['6..4.x187..681.2x.2.713276.669x.252', '6,7,8,6,5'],
/*49*/ ['.6.xx64..5146x897231.x.21265392x9775', '19,17,19,20,17'],
/*50*/ ['334.85413.263314.x.6293921x3.6357647x', '14,14,12,16,10'],
/*51*/ ['4.1..9..513.266..5999769852.2.38x79.x7', '12,10,13,6,10']
];
for(var i = 0; i < inputs.length; ++i) {
(function(src, expected) {
it("should return " + expected + " (" + i + ").", function() {
var actual = yhpg.solve(src);
expect(actual).toBe(expected);
});
})(inputs[i][0], inputs[i][1]);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment