fix: allow makeChange to balance out cassettes
This commit is contained in:
parent
68d5f2b998
commit
4c1be68f2a
1 changed files with 28 additions and 10 deletions
|
|
@ -1,5 +1,5 @@
|
|||
const uuid = require('uuid')
|
||||
const _ = require('lodash')
|
||||
const _ = require('lodash/fp')
|
||||
|
||||
// Custom algorith for two cassettes. For three or more denominations, we'll need
|
||||
// to rethink this. Greedy algorithm fails to find *any* solution in some cases.
|
||||
|
|
@ -63,11 +63,12 @@ function makeChangeDynamic (cassettes, amount) {
|
|||
) {
|
||||
if (cassettes[1].denomination === 0) break
|
||||
if (
|
||||
i * cassettes[0].denomination +
|
||||
j * cassettes[1].denomination ===
|
||||
i * cassettes[0].denomination + j * cassettes[1].denomination ===
|
||||
amountNum &&
|
||||
i <= cassettes[0].count &&
|
||||
j <= cassettes[1].count && i >= 0 && j >= 0
|
||||
j <= cassettes[1].count &&
|
||||
i >= 0 &&
|
||||
j >= 0
|
||||
) {
|
||||
solutions.push([
|
||||
{
|
||||
|
|
@ -108,7 +109,10 @@ function makeChangeDynamic (cassettes, amount) {
|
|||
amountNum &&
|
||||
i <= cassettes[0].count &&
|
||||
j <= cassettes[1].count &&
|
||||
k <= cassettes[2].count && i >= 0 && j >= 0 && k >= 0
|
||||
k <= cassettes[2].count &&
|
||||
i >= 0 &&
|
||||
j >= 0 &&
|
||||
k >= 0
|
||||
) {
|
||||
solutions.push([
|
||||
{
|
||||
|
|
@ -152,7 +156,11 @@ function makeChangeDynamic (cassettes, amount) {
|
|||
i <= cassettes[0].count &&
|
||||
j <= cassettes[1].count &&
|
||||
k <= cassettes[2].count &&
|
||||
l <= cassettes[3].count && i >= 0 && j >= 0 && k >= 0 && l >= 0
|
||||
l <= cassettes[3].count &&
|
||||
i >= 0 &&
|
||||
j >= 0 &&
|
||||
k >= 0 &&
|
||||
l >= 0
|
||||
) {
|
||||
solutions.push([
|
||||
{
|
||||
|
|
@ -182,11 +190,21 @@ function makeChangeDynamic (cassettes, amount) {
|
|||
}
|
||||
}
|
||||
|
||||
const sortedSolutions = _.sortBy(solutions, it => {
|
||||
_.reduce(it, (acc, value) => acc + value.provisioned, 0)
|
||||
})
|
||||
const sortedSolutions = _.sortBy(it => {
|
||||
const arr = []
|
||||
|
||||
const cleanSolution = _.filter(_.head(sortedSolutions), it => it.denomination > 0)
|
||||
for (let la = 0; la < 4; la++) {
|
||||
arr.push(cassettes[la].count - it[la].provisioned)
|
||||
}
|
||||
|
||||
if (arr.length < 2) return Infinity
|
||||
return _.max(arr) - _.min(arr)
|
||||
}, solutions)
|
||||
|
||||
const cleanSolution = _.filter(
|
||||
it => it.denomination > 0,
|
||||
_.head(sortedSolutions)
|
||||
)
|
||||
|
||||
return cleanSolution
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue