-
Notifications
You must be signed in to change notification settings - Fork 0
/
eloquent-robot.js
93 lines (81 loc) · 2.65 KB
/
eloquent-robot.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
const roads = [
"Alice's House-Bob's House", "Alice's House-Cabin",
"Alice's House-Post Office", "Bob's House-Town Hall",
"Daria's House-Ernie's House", "Daria's House-Town Hall",
"Ernie's House-Grete's House", "Grete's House-Farm",
"Grete's House-Shop", "Marketplace-Farm",
"Marketplace-Post Office", "Marketplace-Shop",
"Marketplace-Town Hall", "Shop-Town Hall"
];
function roadsToGraph(roads) {
const graph = Object.create(null);
function addEdge(to, from) {
if (Object.prototype.hasOwnProperty.call(graph, from)) {
graph[from].push(to);
} else {
graph[to].push(from);
}
}
roads.map(road => road.split(' ')).forEach(([from, to]) => {
addEdge(from, to);
addEdge(to, from);
})
return graph;
}
function roadsToGraphFunctional(roads) {
const graph = Object.create(null);
roads.map(road => road.split('-')).forEach(([from, to]) => {
graph[from] = graph[from] ? [...graph[from], to] : [to]
graph[to] = graph[to] ? [...graph[to], from] : [from];
})
return graph;
}
const graph = roadsToGraphFunctional(roads);
console.log(Object.keys(graph));
console.log(graph);
class VillageState {
constructor(place, parcels) {
this.place = place;
this.parcels = parcels;
}
move(destination) {
if (!graph[this.place].includes(destination)) {
return this;
} else {
const parcels = this.parcels.map(p => {
if (this.place !== p.place) return p;
return {place: destination, address: p.address};
}).filter(p => p.place !== p.address)
return new VillageState(destination, parcels);
}
}
static random(parcelCount) {
const parcels = [];
for (let i = 0; i < parcelCount; i += 1) {
const address = randomPick(Object.keys(graph));
let place;
do {
place = randomPick(Object.keys(graph))
} while (place === address);
parcels.push({place, address})
}
return new VillageState("Post Office", parcels)
}
}
let first = new VillageState(
"Post Office",
[{place: "Post Office", address: "Alice's House"}]
);
let next = first.move("Alice's House");
console.log(next.place);
console.log(next.parcels);
console.log(first.place);
function randomPick(array) {
const i = Math.floor(Math.random() * array.length);
return array[i];
}
function randomRobot(state) {
return {direction: randomPick(graph[state.place])};
}
const state = VillageState.random(5);
console.log(state.place, state.parcels);