-
Notifications
You must be signed in to change notification settings - Fork 3
/
office-rotation.js
116 lines (101 loc) · 3.55 KB
/
office-rotation.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
109
110
111
112
113
114
115
116
// UNFINISHED: doesn't quite work yet, but turns out to be obsolete because the shared flat situation dictates only one possible configuration anyway :)
let teams = {};
// only consider T4G fellow teams at this point
require('./input/teams.json').teams.filter(team => team.id < 8).map(team => teams[team.short] = team);
let teamIDs = Object.keys(teams);
n = Math.pow(2, teamIDs.length);
let targetGroupSize = 4;
let groupsOfTargetGroupSize = [];
for (let i = 0; i < n; i++) {
let binary = i.toString(2); // via stackoverflow.com/a/9939785
let zerosToFill = teamIDs.length - binary.length;
for (let j = 0; j < zerosToFill; j++) {
binary = '0' + binary
}
let arr = binary.split('');
if (arr.filter(char => char === '1').length === targetGroupSize) {
let group = [];
for (let j = 0; j < teamIDs.length; j++) {
if (arr[j] === '1') {
group.push(teamIDs[j]);
}
}
groupsOfTargetGroupSize.push(group);
}
}
let cohorts = [];
for (let i = 0; i < groupsOfTargetGroupSize.length / 2; i ++) {
// why is this the case?? but it is correct
let group = groupsOfTargetGroupSize[i];
let counterGroup = groupsOfTargetGroupSize[groupsOfTargetGroupSize.length - 1 - i];
cohorts.push([group, counterGroup]);
}
const getSharedElementsInArrays = (arr1, arr2) => {
return arr1.filter(x => arr2.includes(x));
};
// TEST
for (let i = 0; i < cohorts.length; i ++) {
// via stackoverflow.com/a/33034768
if (getSharedElementsInArrays(cohorts[i][0], cohorts[i][1]).length > 0) {
console.log("TEST FAILED for ", cohorts[i]);
}
}
// FILTER OUT BASED ON CRITERIA
const readlines = require('n-readlines');
people = {};
let line;
let liner = new readlines('input/people.csv');
while (line = liner.next()) {
let parts = line.toString('utf8').split(',');
let id = parts[2]; // first name with first char of last name if necessary
let team = Number(parts[0]);
if (team >= 8) {
continue;
}
people[id] = {
"in_team": team,
"id": id,
"name": parts[3],
"inseparable_from": []
};
}
liner = new readlines('input/blockers/office-rotation-blocker.csv');
while (lineRaw = liner.next()) {
line = lineRaw.toString('utf8');
if (!line || line.length === 0 || line.charAt(0) === '#') {
continue;
}
let parts = line.split(',');
for (let i = 0; i < parts.length; i ++) {
let idSelf = parts[i];
for (let j = i + 1; j < parts.length; j ++) {
let idOther = parts[j];
people[idSelf].inseparable_from.push(idOther);
people[idOther].inseparable_from.push(idSelf);
}
}
}
const getPeopleInGroup = _teamIDs => {
let ppl = [];
for (let i = 0; i < _teamIDs.length; i ++) {
let teamIDinteger = teams[_teamIDs[i]].id;
ppl = [].concat.apply(ppl, Object.keys(people).filter(id => people[id].in_team === teamIDinteger));
}
return ppl;
};
for (let i = 0; i < cohorts.length; i ++) {
let group1 = cohorts[i][0];
let peopleInGroup1 = getPeopleInGroup(group1);
let group2 = cohorts[i][0];
let peopleInGroup2 = getPeopleInGroup(group2);
let containsInseparables = false;
for (let j = 0; j < peopleInGroup1.length; j++) {
let inseparables = getSharedElementsInArrays(people[peopleInGroup1[j]].inseparable_from, peopleInGroup2);
if (inseparables.length > 0) {
containsInseparables = true;
}
}
if (!containsInseparables) {
console.log(cohorts[i][0], " <--> ", cohorts[i][1]);
}
}