-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchallenge-20.ts
41 lines (35 loc) · 887 Bytes
/
challenge-20.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
export function howManyReindeers(
reindeerTypes: Reindeer[],
gifts: Gift[],
): Result[] {
reindeerTypes.sort((a, b) => b.weightCapacity - a.weightCapacity)
return gifts.map(({ country, weight }) => {
const remainingRnds = reindeerTypes.filter(x => x.weightCapacity < weight)
let total = remainingRnds.reduce(
(acc, curr) => (acc += curr.weightCapacity),
0,
)
const reindeers = remainingRnds.map(({ type, weightCapacity }) => {
const num = (weight / total) >> 0
total -= weightCapacity
weight -= num * weightCapacity
return { type, num }
})
return { country, reindeers }
})
}
export interface Reindeer {
type: string
weightCapacity: number
}
export interface Gift {
country: string
weight: number
}
export interface Result {
country: string
reindeers: Array<{
type: string
num: number
}>
}