-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbelief.js
96 lines (87 loc) · 1.98 KB
/
belief.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
/* Key
T => Taxi
B => Bus
U => Underground
F => Ferry
*/
/* Incomplete map of London,
add all of them to play! */
let compactMap = [
'22>TB>34',
'22>TB>23',
'22>T>11',
'23>TB>13',
'13>U>46',
];
let parseEdge = (edge) => {
let [a, ts, b] = edge.split('>');
return [a, ts.split(''), b]
};
import { mapValues } from './util.js';
/* Routes are reflexive,
and each mode of transit
counts as an independent action */
let expandMap = (compact) => {
let map = {};
let insert = (a, t, b) => {
let trans = map[a] = map[a] || {};
let dests = trans[t] = trans[t] || new Set();
dests.add(b);
};
compact
.map(parseEdge)
.forEach(([a, ts, b]) =>
ts.map(t => {
insert(a, t, b);
insert(b, t, a);
})
);
// Turn Sets into arrays
return mapValues(map, ts =>
mapValues(ts, bs => [...bs])
);
};
let map = expandMap(compactMap);
// console.log(map);
/*
crookBeliefStates = { '13': 1 };
detectiveStates = {
Red: '15',
Green: '18',
Yellow: '54',
Blue: '113',
};
transit = 'T';
*/
export let nextBeliefStates = (
crookBeliefStates,
detectiveStates,
transit
) => {
/* A crook can take any action
so long as it doesn't
land on a detective */
let beliefStates = {};
let addBelief = (b, p) => {
beliefStates[b] = (beliefStates[b] || 0) + p;
};
let isDetectiveAt = (b) =>
Object.entries(detectiveStates).some(
([_, position]) => position === b
);
Object.entries(crookBeliefStates).forEach(([a, p]) =>
Object.entries(map[a]).forEach(([t, bs]) =>
/* If transit is known,
ignore any other transits */
!(transit && transit !== t) && bs.forEach(b =>
!isDetectiveAt(b) && addBelief(b, p)
)
)
);
return normalizeBeliefStates(beliefStates);
};
export let normalizeBeliefStates = (beliefStates) => {
let total = Object.entries(beliefStates)
.reduce((sum, [_, p]) => sum + p, 0);
return mapValues(beliefStates, p => p / total);
};