57 lines
2.0 KiB
JavaScript
57 lines
2.0 KiB
JavaScript
import players from "../data/players.json" with { type: "json" };
|
|
import puzzles from "../data/puzzles.json" with { type: "json" };
|
|
|
|
const byId = new Map(players.map((player) => [player.id, player]));
|
|
|
|
function solutionPlayers(puzzle) {
|
|
return Object.entries(puzzle.solution).map(([position, playerId]) => {
|
|
const player = byId.get(playerId);
|
|
if (!player) throw new Error(`${puzzle.id}: missing player ${playerId}`);
|
|
return { slot: position, ...player };
|
|
});
|
|
}
|
|
|
|
function count(items, predicate) {
|
|
return items.filter(predicate).length;
|
|
}
|
|
|
|
function clubGroups(items) {
|
|
return Object.values(
|
|
items.reduce((groups, player) => {
|
|
groups[player.club] ??= [];
|
|
groups[player.club].push(player);
|
|
return groups;
|
|
}, {}),
|
|
);
|
|
}
|
|
|
|
for (const puzzle of puzzles) {
|
|
const lineup = solutionPlayers(puzzle);
|
|
const clueText = puzzle.clues.join(" | ");
|
|
|
|
if (clueText.includes("Two players share the same club.")) {
|
|
const exactPairs = clubGroups(lineup).filter((group) => group.length === 2);
|
|
const biggerGroups = clubGroups(lineup).filter((group) => group.length > 2);
|
|
if (exactPairs.length !== 1 || biggerGroups.length !== 0) {
|
|
throw new Error(`${puzzle.id}: expected exactly one club pair of two`);
|
|
}
|
|
}
|
|
|
|
if (clueText.includes("Four players play in England.")) {
|
|
const total = count(lineup, (player) => player.league === "Premier League");
|
|
if (total !== 4) throw new Error(`${puzzle.id}: expected exactly four Premier League players, got ${total}`);
|
|
}
|
|
|
|
if (clueText.includes("Three players are from Real Madrid.")) {
|
|
const total = count(lineup, (player) => player.club === "Real Madrid");
|
|
if (total !== 3) throw new Error(`${puzzle.id}: expected exactly three Real Madrid players, got ${total}`);
|
|
}
|
|
|
|
if (clueText.includes("Two players are current Premier League players.")) {
|
|
const total = count(lineup, (player) => player.league === "Premier League");
|
|
if (total !== 2) throw new Error(`${puzzle.id}: expected exactly two Premier League players, got ${total}`);
|
|
}
|
|
}
|
|
|
|
console.log("Puzzle data OK");
|