-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
124 lines (108 loc) · 2.82 KB
/
app.ts
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
117
118
119
120
121
122
123
124
import { immerable, produce, Patch } from "immer";
import { permutations } from "iter-tools/es2018";
import { firstBy } from "thenby";
enum Color {
Red,
Yellow,
Blue,
Green
}
const Colors = ["red", "yellow", "blue", "green"];
class Player {
[immerable] = true;
constructor(
public name: string,
public position: number,
public team: number,
public color: Color
) {}
}
function calculateColorDistance(current: Color, desired: Color) {
return (((desired - current) % 4) + 4) % 4;
}
function shuffle<T>(array: T[]) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
const players = [
new Player("alexander", 1, 0, Color.Red),
new Player("cole", 2, 0, Color.Red),
new Player("jake", 3, 0, Color.Red),
new Player("josh", 4, 1, Color.Blue),
new Player("max", 5, 1, Color.Blue),
new Player("mayfield", 6, 1, Color.Blue),
new Player("noah", 7, 2, Color.Yellow),
new Player("tim", 8, 2, Color.Yellow)
];
function run() {
const teamCount =
players.length < 6
? 2
: players.length < 8
? 2 + Math.floor(Math.random() * 2)
: 2 + Math.floor(Math.random() * 3);
console.log("teamCount:", teamCount);
shuffle(players);
for (let i = 0; i < players.length; i++) {
players[i].team = Math.floor((i * teamCount) / players.length);
}
console.log("teamed:", JSON.stringify(players, null, 2));
let options = [];
for (const optionColors of permutations(
[Color.Red, Color.Yellow, Color.Blue, Color.Green],
teamCount
)) {
const option = {
colors: optionColors,
totalChanges: 0,
playerChanges: 0,
patches: [] as Patch[][]
};
for (const player of players) {
let next = player;
let playerPatches: Patch[] = [];
while (next.color !== option.colors[next.team])
next = produce(
next,
draft => {
draft.color = (draft.color + 1) % 4;
},
patches => {
playerPatches.push(...patches);
}
);
if (playerPatches.length > 0) {
option.patches.push(playerPatches);
}
const distance = calculateColorDistance(
player.color,
option.colors[player.team]
);
if (distance > 0) {
option.totalChanges += distance;
option.playerChanges += 1;
}
}
options.push(option);
}
options.sort(
firstBy(it => it.patches.length).thenBy(it =>
(it.patches as Patch[][]).reduce((total, it) => total + it.length, 0)
)
);
options.forEach(option =>
console.log(
option.colors.map(it => Colors[it]),
option.patches,
option.playerChanges,
option.totalChanges,
)
);
function recurse(availableTeams: Color[]) {
return [];
}
}
run();