-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (88 loc) · 2.04 KB
/
index.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* Example dialog
Where is Mr. X?
===============
> 32
Where are the detectives?
=========================
Red: 15
Yellow: 22
Green: 102
Blue: 43
What transit did Mr. X take?
============================
t) Taxi
b) Bus
u) Underground
f) Ferry
m) Mystery
We believe Mr. X is here:
=========================
123 (80%)
13 (12%)
99 (5%)
192 (3%)
*/
import inquirer from 'inquirer';
import { nextBeliefStates } from './belief.js';
import orderBy from 'lodash.orderby';
const detectives = [
'Red',
'Green',
'Yellow',
'Blue',
];
let ask = async (question) =>
(await inquirer.prompt({...question, name:'_'}))._
;
let formatBeliefStates = (beliefStates) =>
orderBy(
Object.entries(beliefStates),
[ '1', s => Number(s[0]) ],
[ 'desc', 'asc' ]
)
.map(([a, p]) => `${a.padStart(3)} (${(100*p).toFixed()}%)`)
.join('\n')
;
let main = async () => {
let crookPosition = await ask({
type: 'input',
message: 'Where is Mr. X?'
});
let crookBeliefStates = { [crookPosition]: 1 };
console.log('');
while (true) {
let detectivePositions = {};
console.log('Where are the detectives?');
console.log('=========================');
for (let detective of detectives) {
detectivePositions[detective] =
await ask({
type: 'input',
message: `${detective}:`,
});
}
console.log('');
let transit = await ask({
type: 'expand',
message: 'What transit did Mr. X take?',
choices: [
{ key: 't', value: 'T', name: 'Taxi' },
{ key: 'b', value: 'B', name: 'Bus' },
{ key: 'u', value: 'U', name: 'Underground' },
{ key: 'f', value: 'F', name: 'Ferry' },
{ key: 'm', value: '', name: 'Mystery' },
]
});
console.log('');
console.log('We believe Mr. X is here:');
console.log('=========================');
crookBeliefStates = nextBeliefStates(
crookBeliefStates,
detectivePositions,
transit
);
console.log(formatBeliefStates(crookBeliefStates));
console.log('');
}
};
main();