You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.1 KiB
JavaScript

#!/usr/bin/env node
const { fromJSON } = require('./buckets')
const usage = `main.js target bucket [bucket...]
target: volume of bucket to search for
bucket: volume of bucket
`
function bail() {
console.log(usage);
process.exit(1)
}
const args = [...process.argv].slice(2)
if (args.length < 2) {
bail();
}
const intArgs = args.map(arg => Number.parseInt(arg, 10))
const hasNaN = intArgs.find(arg => Number.isNaN(arg))
if (hasNaN) {
bail();
}
const target = intArgs[0];
const bucketsJSON = intArgs.slice(1).map(capacity => ({ capacity }))
const buckets = fromJSON({ buckets: bucketsJSON })
const result = buckets.findTarget(target)
if (result.length === 0) {
console.log('No results')
}
if (result.length === 1) {
console.log('1 solution')
}
if (result.length > 1) {
console.log(`${result.length} solutions`)
}
result.forEach((actions, i) => {
console.log(`solution ${i + 1}`)
const copy = fromJSON(buckets);
actions.forEach(action => {
copy.performAction(action)
console.log(`${action.string()} resulting in ${copy.string()}`)
})
});