-
Notifications
You must be signed in to change notification settings - Fork 2
/
coinflip.js
87 lines (81 loc) · 1.88 KB
/
coinflip.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// This is a basically a direct copy from the source.
class BadRng {
x;
m = 1024;
a = 341;
c = 1;
constructor() {
this.x = 0;
this.reset();
}
step() {
this.x = (this.a * this.x + this.c) % this.m;
}
random() {
this.step();
return this.x / this.m;
}
reset() {
this.x = 0;
}
}
/** @param {NS} ns */
export async function main(ns) {
ns.disableLog("ALL");
if (!ns.args[0] || typeof ns.args[0] !== "string") {
ns.tprintf("ERROR: Need initial sequence of flips to align sequence!");
ns.tprintf('Specify flips with "run coinflip.js HTHTTH" etc.');
return;
}
const chr = [];
const rng = new BadRng();
for (let i = 0; i < 1024; ++i) {
chr[i] = rng.random() < 0.5 ? "H" : "T";
}
let haystack = chr.join("");
haystack += haystack.slice(0, ns.args[0].length - 1);
const re = RegExp(ns.args[0], "g");
let goodIdx, result;
let numResults = 0;
while ((result = re.exec(haystack))) {
goodIdx = result.index;
re.lastIndex = result.index + 1;
numResults++;
}
if (numResults !== 1) {
ns.tprintf(
"ERROR: Needed 1 match, but found %d matching sequences",
numResults
);
if (numResults === 0) {
ns.tprintf("You made a mistake, or there is a program error.");
} else {
ns.tprintf("Try gathering more coin flips.");
}
return;
}
const cut = goodIdx + ns.args[0].length;
haystack = haystack.slice(cut, 1024) + haystack.slice(0, cut);
ns.clearLog();
ns.tail();
await ns.asleep(5);
ns.resizeTail(389, 673);
const output = [];
let line = "";
for (let i = 0; i < 256; ++i) {
line += haystack.slice(i * 4, i * 4 + 4);
if (i % 8 == 7) {
output.push(line);
if (i % 32 == 31) {
output.push("");
}
line = "";
continue;
}
line += " ";
if (i % 4 == 3) {
line += " ";
}
}
ns.printf("%s", output.join("\n"));
}