aboutsummaryrefslogtreecommitdiff
path: root/entries/braxtonhall/hitl2/fib.js
blob: e050d967145a009f038dd30bbdc98f396bdcc8b1 (plain) (blame)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const readline = require('readline');

const random = (lo, hi) => lo + Math.floor(Math.random() * (hi - lo));
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const rsleep = (lo, hi) => sleep(random(lo, hi));

const interface = readline.createInterface({
	input: process.stdin,
	output: process.stdout,
});

const prompt = (question) =>
	new Promise((resolve) =>
		interface.question(`${blue(question)}\n> `, resolve));

const coloured = (colour) => (str) => `${colour}${reset(str)}`;
const yellow = coloured("\x1b[33m");
const blue = coloured("\x1b[34m");
const green = coloured("\x1b[32m");
const reset = (str) => `${str}\x1b[0m`;

const add = (a, b) => compute(a, b, "+");

const hedges = [
	"um. okay. so,",
	"right. next. uh,",
	"hmm",
	"okay. so,",
	"um. okay.",
	"uh okay..",
	"so..",
];
const confirmations = [
	"o right. ofc.",
	"nice.",
	"thank u.",
	"thanks.",
	"um right. yes.",
	"right.",
];
const innumerables = [
	"yeah i don't think we can do that one",
	"that's not great for fib",
	"um no sorry i can't help with that one",
	"um????? no my b i can't help with that one",
	"oh no my bad no i can't help u with that",
	"sorry not that one",
	"yaaa not that one though",
];
const dones = [
	"we're done aren't we?",
	"oh! that's it..?",
	"all ... done??????",
	"i think it's over",
	"no more computation?",
	"oh!",
	"wait you did it all.",
	"oh that's it?",
];
const thinkings = [
	"let me just think a second..",
	"hold on..",
	"i just need a moment",
	"uh wait",
	"just looking at the data",
	"sorry - looking over things",
	"got everything just one sec",
	"let me look at things",
	"let me think a moment",
	"just thinking ...",
];
const rarray = (array) => array[random(0, array.length)];
const hedge = () => rarray(hedges);
const confirmation = () => rarray(confirmations);
const innumerable = () => rarray(innumerables);
const done = () => rarray(dones);
const thinking = () => rarray(thinkings);

const rentries = (map) => rarray(Array.from(map.entries()));

const say = (msg, before, after) =>
	sleep(before)
		.then(() => console.log(...msg))
		.then(() => sleep(after));

const fast = (...msg) => say(msg, random(30, 100), random(30, 100));
const slow = (...msg) => say(msg, random(400, 2000), random(200, 1000));

const compute = (a, b, op) =>
	slow(hedge())
		.then(() => prompt(`what is ${a} ${op} ${b}?`))
		.then((value) => slow(confirmation()).then(() => value));

const build = (n, map) => {
	if (n <= 0) {
		const zero = () => Promise.resolve();
		map.set(zero, 0);
		return zero;
	} else if (n === 1) {
		const one = () => Promise.resolve();
		map.set(one, 1);
		return one;
	} else {
		const sub1 = build(n - 1, map);
		const sub2 = build(n - 2, map);
		const nth = () =>
			(map.get(nth) === null && (map.get(sub1) !== null && map.get(sub2) !== null))
				? add(map.get(sub2), map.get(sub1))
					.then((value) => Promise.resolve(map.set(nth, value)).then(() => value))
				: Promise.resolve();
		map.set(nth, null);
		return nth;
	};
};

const fill = (map, top) =>
	map.get(top) === null
		? rentries(map)[0]().then(() => fill(map, top))
		: Promise.resolve(map.get(top));

const hitlfib = (n) =>
	Promise.resolve(new Map())
		.then((map) => Promise.resolve(build(n, map))
			.then((top) => fill(map, top)));

const hitlfibiter = (n, a = 0, b = 1) =>
	n === 0
		? Promise.resolve(a)
		: n === 1
			? Promise.resolve(b)
			: add(a, b).then((result) => hitlfibiter(n - 1, b, result));

const number = () =>
	prompt("give me a number")
		.then((str) => str.trim())
		.then((trimmed) => Promise.resolve(Number(trimmed))
			.then((n) => (trimmed !== "" && n !== null && isFinite(n))
				? n
				: slow(hedge()).then(() => slow(innumerable())).then(number)));

const choose = () =>
	prompt("would you like to use my iteration optimization??? yes????")
		.then((response) => ["y", "yes"].includes(response.trim().toLowerCase()))
		.then((should) => should
			? fast("yes!").then(() => hitlfibiter)
			: fast("oh.").then(() => slow("okay.")).then(() => slow("we can take it slow.")).then(() => hitlfib));

const flow = () =>
	slow("\nwhat's the fib you'd like to compute? i can help u out")
		.then(number)
		.then((n) => slow(confirmation())
			.then(() => fast(hedge()))
			.then(() => slow(`i will help u compute ${green(`fib ${n}`)}`))
			.then(choose)
			.then((fib) => fib(n))
			.then((fibn) => slow(hedge())
				.then(() => fast(done()))
				.then(() => fast(thinking()))
				.then(() => rsleep(Math.min(1000, n * 100), Math.max(n * 500, 1000)))
				.then(() => slow(green(`fib ${n} is ${fibn}`)))))
		.then(() => rsleep(1000, 4000));

const loop = () => flow().then(loop);

loop();