-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcityBlocks.js
More file actions
72 lines (61 loc) · 2.12 KB
/
cityBlocks.js
File metadata and controls
72 lines (61 loc) · 2.12 KB
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
/////////////////////////////////////////////////////////////
// City Blocks
// --------------------------
// Citizens are spread out over a 2d city grid represented by a 2d matrix.
// Find the point on the grid that will require the least distance moved if
// they were to all meet (movement is per citizen, so 2 people moving one block
// is considered as covering a distance of 2).
//
/////////////////////////////////////////////////////////////
var getOptimalMeetingPoint = function () {
// TODO: Implement
};
/////////////////////////////////////////////////////////////
// HELPERS
/////////////////////////////////////////////////////////////
// Take these!
/////////////////////////////////////////////////////////////
var cityBlockTestCases = range(10, 0)
.map(function () {
return buildMatrix(9, 15);
});
//////////////////////////////////////////////////
// MATRIX BUILDER
//////////////////////////////////////////////////
// Returns an array of rowSize length, populated with elm
function range (rowSize, elm) {
var result = [];
for (var i = 0; i < rowSize; i++) {
if (Array.isArray(elm)) {
result.push(elm.slice());
} else {
result.push(elm);
}
}
return result;
}
// randomize takes a matrix, it's size, number of random elms and elms
function randomize (matrix, size, num) {
if (num === 0) {
return matrix;
} else if (num > size * size) {
return "Number exceeds size of matrix";
}
// generate random row and col
var row = Math.floor(Math.random() * (size - 1));
var col = Math.floor(Math.random() * (size - 1));
// generate random number of citizens on square
var elm = Math.floor(Math.random() * 5);
// If we haven't placed a person at that coord, set a person on that coord,
// decrement num, then recurse up.
if (matrix[row][col] !== 0) {
return randomize(matrix.slice(), size, num);
} else {
matrix[row][col] = elm;
return randomize(matrix.slice(), size, num - 1);
}
};
// builds 2d array where num is the number of random pieces on the board
function buildMatrix (size, num) {
return randomize(range(size, range(size, 0)).slice(), size, num);
};