Skip to content

Instantly share code, notes, and snippets.

@json2d
Created April 14, 2017 19:40
Show Gist options
  • Save json2d/c3275ea3560d27a81261324b1a1ae10d to your computer and use it in GitHub Desktop.
Save json2d/c3275ea3560d27a81261324b1a1ae10d to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const solveWith = require('./lib/solve-with')
const bigInt = require('jsbn').BigInteger
var stall = (input) => {
var [N,K] = input.split(' ')
// N = new bigInt(N)
K = new bigInt(K)
var empties = {[N]: bigInt.ONE}
while(K.compareTo(bigInt.ZERO) > 0) {
var best = Object.keys(empties).reduce((longestChain,chain) => longestChain.max(new bigInt(chain)),bigInt.ZERO)
var count = empties[best]
delete empties[best]
var left = best.subtract(bigInt.ONE);
var a = left.divide(new bigInt('2'));
var b = left.subtract(a);
var max = a.max(b)
var min = a.min(b)
console.log('count:',''+count,'max:',''+max,'min:',''+min)
if(K.compareTo(count) <= 0) {
return max + " " + min
}
K = K.subtract(count);
empties[max]===undefined ? empties[max] = count : empties[max] = empties[max].add(count);
empties[min]===undefined ? empties[min] = count : empties[min] = empties[min].add(count);
}
console.error('no solution')
}
solveWith(stall,process.argv[2],process.argv[3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment