-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed.js
More file actions
52 lines (45 loc) · 1.69 KB
/
Copy pathseed.js
File metadata and controls
52 lines (45 loc) · 1.69 KB
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
42
43
44
45
46
47
48
49
50
51
52
export const generateSeed = seedCode =>
seedFunctions [seedCode.slice (0, 2)] ? Uint8Array.from (
seedFunctions [seedCode.slice (0, 2)] (seedCode.slice (2))
): console.error ('invalid seed code: ' + seedCode) || [0]
const superDragon = (fillers = ['1', '0'], strings = []) => iter =>
iter <= -1 ? strings [0] : superDragon (
fillers,
fillers.map (filler => strings.join(filler))
) (iter - 1)
const superKolakosky = (seed = [1, 2], prev) => iter =>
iter <= 0 ? prev ?? seed : superKolakosky (
seed,
runAlong (seed, prev ?? seed)
) (iter - 1)
const superCantor = seed => iter =>
iter ?
superCantor (seed) (iter - 1).flatMap (
x => seed.map (y => Math.min (x, y))
) :
[Math.max(...seed)]
const runAlong = (seed, prev = []) =>
prev.flatMap ((repCount, idx) =>
new Array (repCount).fill (seed [idx % seed.length])
)
const recurSubst = (lookup, prev = '0') => iter =>
iter <= 0 ? prev : recurSubst (
lookup,
prev.split ('').map (idx => lookup[Number.parseInt(idx)] ?? '').join ('')
) (iter - 1)
const randomDigits = (threshold = .5) => lengthLog2 => (
new Array (Math.ceil (2 ** Number.parseFloat (lengthLog2)))
.fill (0)
.map (() => Math.random () > threshold ? 1 : 0)
)
// todo: more parameterized seeds in general would be nice
const seedFunctions = {
tm: recurSubst (['01', '10']), // Thue-Morse sequence
rb: recurSubst (['1', '10']), // rabbit sequence (aka fibonacci word)
cr: recurSubst (['00', '1'], '010'), // core (single 1 surrounded by 0s)
ts: recurSubst (['00', '11'], '01'), // transition (0 on one side, ones at the other)
dc: superDragon (['1', '0']),
kk: iter => superKolakosky ([1, 2]) (iter).map (x => x -1),
ct: superCantor ([1, 0, 1]),
rd: randomDigits (.5)
}