-
Notifications
You must be signed in to change notification settings - Fork 2
/
inventory.ts
38 lines (28 loc) · 878 Bytes
/
inventory.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
// Calculates the inventory sequence as described in
// https://www.youtube.com/watch?v=rBU9E-ZOZAI.
export type Inventory = number[]
export function termCount<T>(array: readonly T[], item: T) {
let count = 0
array.forEach((value) => value === item && count++)
return count
}
/** This function mutates its input array. */
export function takeInventory(sequence: number[]) {
const inventory: Inventory = []
let lastCount = -1
for (let index = 0; lastCount != 0; index++) {
lastCount = termCount(sequence, index)
inventory.push(lastCount)
sequence.push(lastCount)
}
return inventory
}
export function repeat(max: number) {
const result: number[] = []
const inventories: Inventory[] = []
for (let index = 0; index < max; index++) {
const next = takeInventory(result)
inventories.push(next)
}
return { result, inventories }
}