Skip to content

Commit 57b3758

Browse files
committed
optimise wc predicate
1 parent 36a90bd commit 57b3758

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

src/engine/predicates/wc/index.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ export default (year: number, teams: readonly Team[]): Predicate<Team> => {
3232

3333
const confMinMaxEntries = Object.entries(confMinMax);
3434

35+
const numMaxGroupsByConf = mapValues(berthsByConfederation, v => {
36+
const r = v % numGroups;
37+
return r === 0 ? numGroups : r;
38+
});
39+
3540
return (picked, groups, groupIndex) => {
3641
const group = groups[groupIndex];
3742
const currentPotIndex = getSmallestArrayLength(groups);
@@ -42,7 +47,7 @@ export default (year: number, teams: readonly Team[]): Predicate<Team> => {
4247

4348
const virtualGroup = [...group, picked] as const;
4449
const numRemainingTeams = groupSize - virtualGroup.length;
45-
return confMinMaxEntries.every(([conf, [min, max]]) => {
50+
const isGroupPossible = confMinMaxEntries.every(([conf, [min, max]]) => {
4651
const numConfTeams = sumBy(virtualGroup, team => {
4752
// @ts-expect-error
4853
const m = team.confederation
@@ -54,5 +59,31 @@ export default (year: number, teams: readonly Team[]): Predicate<Team> => {
5459
});
5560
return numConfTeams <= max && numConfTeams + numRemainingTeams >= min;
5661
});
62+
if (!isGroupPossible) {
63+
return false;
64+
}
65+
66+
const virtualGroups = groups.with(groupIndex, virtualGroup);
67+
const areGroupsPossible = confMinMaxEntries.every(([conf, [, max]]) => {
68+
const numMaxedOutGroups = sumBy(virtualGroups, g => {
69+
const numConfTeams = sumBy(g, team => {
70+
// @ts-expect-error
71+
const m = team.confederation
72+
? (team as NationalTeam).confederation === conf
73+
: (team as UnknownNationalTeam).confederations.has(
74+
conf as Confederation,
75+
);
76+
return m ? 1 : 0;
77+
});
78+
return numConfTeams === max ? 1 : 0;
79+
});
80+
return numMaxedOutGroups <= numMaxGroupsByConf[conf as Confederation];
81+
});
82+
83+
if (!areGroupsPossible) {
84+
return false;
85+
}
86+
87+
return true;
5788
};
5889
};

0 commit comments

Comments
 (0)