refactor: simplify reducing solution
This commit is contained in:
parent
9ef9a70c39
commit
16e57ee3ab
1 changed files with 3 additions and 14 deletions
|
|
@ -129,24 +129,13 @@ const getSolution = (units, amount) => {
|
|||
|
||||
const solutionToOriginalUnits = (solution, units) => {
|
||||
const billsToAssign = (count, left) => _.clamp(0, count)(_.isNaN(left) || _.isNil(left) ? 0 : left)
|
||||
|
||||
const billsLeft = _.flow(
|
||||
_.map(([denomination, provisioned]) => [BN(denomination), provisioned]),
|
||||
_.reduce((acc, value) => {
|
||||
acc[value[0]] = (acc[value[0]] || BN(0)).plus(value[1])
|
||||
return acc
|
||||
},
|
||||
{}
|
||||
)
|
||||
)(solution)
|
||||
|
||||
return _.map(
|
||||
const billsLeft = Object.fromEntries(solution)
|
||||
return units.map(
|
||||
({ count, name, denomination }) => {
|
||||
const provisioned = billsToAssign(count, billsLeft[denomination])
|
||||
billsLeft[denomination] -= provisioned
|
||||
return { name, denomination, provisioned }
|
||||
},
|
||||
units
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue