initial commit
This commit is contained in:
@@ -1,33 +1,18 @@
|
||||
#!/usr/bin/env node
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Swedish-style crossword generator (mask + fill)
|
||||
* Spec:
|
||||
* - Grid 9x8
|
||||
* - Letter cells: '#' (empty) or 'A'..'Z' (fixed)
|
||||
* - Clue cells: '1'..'4' indicating direction:
|
||||
* 1=up, 2=right, 3=down, 4=left
|
||||
* - Word length: 2..8
|
||||
* - Words from word-list.txt (one per line, uppercase A-Z)
|
||||
*
|
||||
* Output:
|
||||
* - GENERATED MASK (digits + #)
|
||||
* - FILLED PUZZLE (digits + letters)
|
||||
* - clue -> word mapping "r,c:d = WORD"
|
||||
*/
|
||||
|
||||
const { exportFormatFromFilled } = require("./export_format");
|
||||
const fs = require("fs");
|
||||
|
||||
const W = 9, H = 8;
|
||||
const MIN_LEN = 2, MAX_LEN = 8;
|
||||
|
||||
const DIRS = {
|
||||
"1": [-1, 0],
|
||||
"2": [0, 1],
|
||||
"3": [1, 0],
|
||||
"4": [0, -1],
|
||||
"1": [-1, 0], // up
|
||||
"2": [0, 1], // right
|
||||
"3": [1, 0], // down
|
||||
"4": [0, -1], // left
|
||||
};
|
||||
|
||||
const IS_DIGIT = (ch) => ch >= "1" && ch <= "4";
|
||||
const IS_LETTER = (ch) => ch >= "A" && ch <= "Z";
|
||||
const IS_LETTER_CELL = (ch) => ch === "#" || IS_LETTER(ch);
|
||||
@@ -39,14 +24,14 @@ function usage() {
|
||||
Defaults:
|
||||
--seed 1
|
||||
--pop 18
|
||||
--gens 220
|
||||
--tries 25
|
||||
--gens 100
|
||||
--tries 50
|
||||
--words ./word-list.txt
|
||||
`);
|
||||
}
|
||||
|
||||
function parseArgs(argv) {
|
||||
const out = {seed: 1, pop: 18, gens: 220, tries: 25, wordsPath: "./word-list.txt"};
|
||||
const out = {seed: 1, pop: 18, gens: 100, tries: 50, wordsPath: "./word-list.txt"};
|
||||
for (let i = 2; i < argv.length; i++) {
|
||||
const a = argv[i];
|
||||
const v = argv[i + 1];
|
||||
@@ -64,7 +49,7 @@ function parseArgs(argv) {
|
||||
return out;
|
||||
}
|
||||
|
||||
/** seeded RNG (xorshift32) */
|
||||
/** Seeded RNG (xorshift32) */
|
||||
function makeRng(seed) {
|
||||
let x = (seed >>> 0) || 1;
|
||||
return {
|
||||
@@ -84,31 +69,29 @@ function makeRng(seed) {
|
||||
float() {
|
||||
return this.nextU32() / 0xFFFFFFFF;
|
||||
},
|
||||
pick(arr) {
|
||||
return arr[this.int(0, arr.length - 1)];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function deepCopyGrid(g) {
|
||||
return g.map(r => r.slice());
|
||||
}
|
||||
|
||||
function gridToString(g) {
|
||||
return g.map(r => r.join("")).join("\n");
|
||||
}
|
||||
function clamp(x, a, b) { return Math.max(a, Math.min(b, x)); }
|
||||
|
||||
function makeEmptyGrid() {
|
||||
return Array.from({length: H}, () => Array.from({length: W}, () => "#"));
|
||||
}
|
||||
|
||||
/** Load words and build fast length+pos index */
|
||||
function deepCopyGrid(g) { return g.map(r => r.slice()); }
|
||||
|
||||
function gridToString(g) { return g.map(r => r.join("")).join("\n"); }
|
||||
|
||||
function renderHuman(g) {
|
||||
return g.map(row => row.map(ch => IS_DIGIT(ch) ? " " : ch).join("")).join("\n");
|
||||
}
|
||||
|
||||
/** --- Words / index --- */
|
||||
function loadWords(wordsPath) {
|
||||
let raw = "";
|
||||
try {
|
||||
raw = fs.readFileSync(wordsPath, "utf8");
|
||||
} catch {
|
||||
// fallback
|
||||
raw = "EU\nUUR\nAUTO\nBOOM\nHUIS\nKAT\nZEE\nRODE\nDRAAD\nKENNIS\nNETWERK\nPAKTE\n";
|
||||
}
|
||||
|
||||
@@ -119,8 +102,12 @@ function loadWords(wordsPath) {
|
||||
|
||||
// index[len] = { words: string[], pos: Array(len) of [26 arrays of indices] }
|
||||
const index = new Map();
|
||||
const lenCounts = new Map();
|
||||
|
||||
for (const w of words) {
|
||||
const L = w.length;
|
||||
lenCounts.set(L, (lenCounts.get(L) || 0) + 1);
|
||||
|
||||
if (!index.has(L)) {
|
||||
const pos = Array.from({length: L}, () =>
|
||||
Array.from({length: 26}, () => [])
|
||||
@@ -134,7 +121,8 @@ function loadWords(wordsPath) {
|
||||
entry.pos[i][w.charCodeAt(i) - 65].push(idx);
|
||||
}
|
||||
}
|
||||
return {words, index};
|
||||
|
||||
return {words, index, lenCounts};
|
||||
}
|
||||
|
||||
function intersectSorted(a, b) {
|
||||
@@ -152,17 +140,17 @@ function intersectSorted(a, b) {
|
||||
return out;
|
||||
}
|
||||
|
||||
function candidateIndicesForPattern(indexEntry, pattern /* array char|null */) {
|
||||
/** returns {indices?: number[], count: number} WITHOUT allocating huge arrays */
|
||||
function candidateInfoForPattern(entry, pattern /* array char|null */) {
|
||||
const lists = [];
|
||||
for (let i = 0; i < pattern.length; i++) {
|
||||
const ch = pattern[i];
|
||||
if (ch && IS_LETTER(ch)) {
|
||||
lists.push(indexEntry.pos[i][ch.charCodeAt(0) - 65]);
|
||||
lists.push(entry.pos[i][ch.charCodeAt(0) - 65]);
|
||||
}
|
||||
}
|
||||
if (lists.length === 0) {
|
||||
// all candidates
|
||||
return Array.from({length: indexEntry.words.length}, (_, i) => i);
|
||||
return {indices: null, count: entry.words.length}; // unconstrained
|
||||
}
|
||||
lists.sort((a, b) => a.length - b.length);
|
||||
let cur = lists[0];
|
||||
@@ -170,16 +158,17 @@ function candidateIndicesForPattern(indexEntry, pattern /* array char|null */) {
|
||||
cur = intersectSorted(cur, lists[k]);
|
||||
if (cur.length === 0) break;
|
||||
}
|
||||
return cur;
|
||||
return {indices: cur, count: cur.length};
|
||||
}
|
||||
|
||||
/** Slot extraction: from each clue cell (digit), read letters away while letter-cells (# or A-Z) */
|
||||
/** --- Slots --- */
|
||||
function extractSlots(grid) {
|
||||
const slots = [];
|
||||
for (let r = 0; r < H; r++) {
|
||||
for (let c = 0; c < W; c++) {
|
||||
const d = grid[r][c];
|
||||
if (!IS_DIGIT(d)) continue;
|
||||
|
||||
const [dr, dc] = DIRS[d];
|
||||
let rr = r + dr, cc = c + dc;
|
||||
if (rr < 0 || rr >= H || cc < 0 || cc >= W) continue;
|
||||
@@ -188,131 +177,19 @@ function extractSlots(grid) {
|
||||
const cells = [];
|
||||
while (rr >= 0 && rr < H && cc >= 0 && cc < W) {
|
||||
const ch = grid[rr][cc];
|
||||
if (!IS_LETTER_CELL(ch)) break; // stops at clue digits (or other non-letter)
|
||||
if (!IS_LETTER_CELL(ch)) break;
|
||||
cells.push([rr, cc]);
|
||||
rr += dr;
|
||||
cc += dc;
|
||||
if (cells.length > MAX_LEN) break;
|
||||
}
|
||||
slots.push({
|
||||
clue: [r, c, d],
|
||||
dir: d,
|
||||
cells,
|
||||
len: cells.length
|
||||
});
|
||||
|
||||
slots.push({clue: [r, c, d], dir: d, cells, len: cells.length});
|
||||
}
|
||||
}
|
||||
return slots;
|
||||
}
|
||||
|
||||
/** Fitness: penalty-based mask evaluation + dictionary-based feasibility hints */
|
||||
function fitness(grid, dictIndex) {
|
||||
let penalty = 0;
|
||||
|
||||
// 1) clue density (avoid "all digits")
|
||||
let clueCount = 0, letterCount = 0;
|
||||
for (let r = 0; r < H; r++) for (let c = 0; c < W; c++) {
|
||||
const ch = grid[r][c];
|
||||
if (IS_DIGIT(ch)) clueCount++;
|
||||
else if (IS_LETTER_CELL(ch)) letterCount++;
|
||||
}
|
||||
const targetClues = Math.round(W * H * 0.25); // ~18
|
||||
penalty += 8 * Math.abs(clueCount - targetClues);
|
||||
|
||||
// 2) slots + length + candidate viability
|
||||
const slots = extractSlots(grid);
|
||||
|
||||
if (slots.length === 0) return 1e9;
|
||||
|
||||
// coverage counts per letter cell: horizontal vs vertical
|
||||
const covH = Array.from({length: H}, () => Array(W).fill(0));
|
||||
const covV = Array.from({length: H}, () => Array(W).fill(0));
|
||||
|
||||
for (const s of slots) {
|
||||
const horizontal = (s.dir === "2" || s.dir === "4");
|
||||
if (s.len < MIN_LEN) penalty += 8000;
|
||||
else if (s.len > MAX_LEN) penalty += 8000 + (s.len - MAX_LEN) * 500;
|
||||
|
||||
// candidate count hint (not full fill, but strong signal)
|
||||
if (s.len >= MIN_LEN && s.len <= MAX_LEN && dictIndex && dictIndex.has(s.len)) {
|
||||
const entry = dictIndex.get(s.len);
|
||||
const pattern = s.cells.map(([r, c]) => {
|
||||
const ch = grid[r][c];
|
||||
return IS_LETTER(ch) ? ch : null;
|
||||
});
|
||||
const candIdx = candidateIndicesForPattern(entry, pattern);
|
||||
const n = candIdx.length;
|
||||
if (n === 0) penalty += 12000; // impossible slot
|
||||
else penalty += Math.floor(400 / Math.log2(n + 2)); // prefer higher branching
|
||||
} else {
|
||||
// no words of that length available
|
||||
if (s.len >= MIN_LEN && s.len <= MAX_LEN) penalty += 12000;
|
||||
}
|
||||
|
||||
// mark coverage
|
||||
for (const [r, c] of s.cells) {
|
||||
if (horizontal) covH[r][c] += 1;
|
||||
else covV[r][c] += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// 3) coverage penalties per letter cell
|
||||
for (let r = 0; r < H; r++) for (let c = 0; c < W; c++) {
|
||||
const ch = grid[r][c];
|
||||
if (!IS_LETTER_CELL(ch)) continue;
|
||||
const h = covH[r][c], v = covV[r][c];
|
||||
if (h === 0 && v === 0) penalty += 1500;
|
||||
else if (h > 0 && v > 0) penalty += 0;
|
||||
else if (h + v === 1) penalty += 200;
|
||||
else penalty += 600; // multiple same-direction passes
|
||||
}
|
||||
|
||||
// 4) clue clustering penalty (8-connected components)
|
||||
const seen = Array.from({length: H}, () => Array(W).fill(false));
|
||||
const nbrs8 = [
|
||||
[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]
|
||||
];
|
||||
for (let r = 0; r < H; r++) for (let c = 0; c < W; c++) {
|
||||
if (!IS_DIGIT(grid[r][c]) || seen[r][c]) continue;
|
||||
const q = [[r, c]];
|
||||
seen[r][c] = true;
|
||||
let size = 0;
|
||||
while (q.length) {
|
||||
const [x, y] = q.pop();
|
||||
size++;
|
||||
for (const [dr, dc] of nbrs8) {
|
||||
const nx = x + dr, ny = y + dc;
|
||||
if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
|
||||
if (seen[nx][ny]) continue;
|
||||
if (!IS_DIGIT(grid[nx][ny])) continue;
|
||||
seen[nx][ny] = true;
|
||||
q.push([nx, ny]);
|
||||
}
|
||||
}
|
||||
if (size >= 2) penalty += (size - 1) * 120;
|
||||
}
|
||||
|
||||
// 5) dead-end-ish penalty: letter cell surrounded by non-letters on 3+ sides
|
||||
const nbrs4 = [[-1, 0], [1, 0], [0, -1], [0, 1]];
|
||||
for (let r = 0; r < H; r++) for (let c = 0; c < W; c++) {
|
||||
if (!IS_LETTER_CELL(grid[r][c])) continue;
|
||||
let walls = 0;
|
||||
for (const [dr, dc] of nbrs4) {
|
||||
const rr = r + dr, cc = c + dc;
|
||||
if (rr < 0 || rr >= H || cc < 0 || cc >= W) {
|
||||
walls++;
|
||||
continue;
|
||||
}
|
||||
const ch = grid[rr][cc];
|
||||
if (!IS_LETTER_CELL(ch)) walls++;
|
||||
}
|
||||
if (walls >= 3) penalty += 400;
|
||||
}
|
||||
|
||||
return penalty;
|
||||
}
|
||||
|
||||
/** Helpers to ensure a clue has room for at least MIN_LEN */
|
||||
function hasRoomForClue(grid, r, c, d) {
|
||||
const [dr, dc] = DIRS[d];
|
||||
let rr = r + dr, cc = c + dc;
|
||||
@@ -325,16 +202,105 @@ function hasRoomForClue(grid, r, c, d) {
|
||||
return run >= MIN_LEN;
|
||||
}
|
||||
|
||||
/** Random initialization: mostly letters, some clues with room */
|
||||
/** --- FAST mask fitness (structural only) --- */
|
||||
function maskFitness(grid, lenCounts) {
|
||||
let penalty = 0;
|
||||
|
||||
// clue density (avoid all digits)
|
||||
let clueCount = 0;
|
||||
for (let r = 0; r < H; r++) for (let c = 0; c < W; c++) {
|
||||
if (IS_DIGIT(grid[r][c])) clueCount++;
|
||||
}
|
||||
const targetClues = Math.round(W * H * 0.25); // ~18
|
||||
penalty += 8 * Math.abs(clueCount - targetClues);
|
||||
|
||||
const slots = extractSlots(grid);
|
||||
if (slots.length === 0) return 1e9;
|
||||
|
||||
// coverage counts per letter cell: horiz vs vert
|
||||
const covH = Array.from({length: H}, () => Array(W).fill(0));
|
||||
const covV = Array.from({length: H}, () => Array(W).fill(0));
|
||||
|
||||
for (const s of slots) {
|
||||
const horiz = (s.dir === "2" || s.dir === "4");
|
||||
|
||||
if (s.len < MIN_LEN) penalty += 8000;
|
||||
if (s.len > MAX_LEN) penalty += 8000 + (s.len - MAX_LEN) * 500;
|
||||
|
||||
// dictionary availability only (cheap)
|
||||
if (s.len >= MIN_LEN && s.len <= MAX_LEN) {
|
||||
if (!lenCounts.get(s.len)) penalty += 12000;
|
||||
}
|
||||
|
||||
for (const [r, c] of s.cells) {
|
||||
if (horiz) covH[r][c] += 1;
|
||||
else covV[r][c] += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// coverage penalties per letter cell
|
||||
for (let r = 0; r < H; r++) for (let c = 0; c < W; c++) {
|
||||
if (!IS_LETTER_CELL(grid[r][c])) continue;
|
||||
const h = covH[r][c], v = covV[r][c];
|
||||
if (h === 0 && v === 0) penalty += 1500;
|
||||
else if (h > 0 && v > 0) penalty += 0;
|
||||
else if (h + v === 1) penalty += 200;
|
||||
else penalty += 600;
|
||||
}
|
||||
|
||||
// clue clustering (8-connected)
|
||||
const seen = Array.from({length: H}, () => Array(W).fill(false));
|
||||
const nbrs8 = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]];
|
||||
for (let r = 0; r < H; r++) for (let c = 0; c < W; c++) {
|
||||
if (!IS_DIGIT(grid[r][c]) || seen[r][c]) continue;
|
||||
const stack = [[r, c]];
|
||||
seen[r][c] = true;
|
||||
let size = 0;
|
||||
while (stack.length) {
|
||||
const [x, y] = stack.pop();
|
||||
size++;
|
||||
for (const [dr, dc] of nbrs8) {
|
||||
const nx = x + dr, ny = y + dc;
|
||||
if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
|
||||
if (seen[nx][ny]) continue;
|
||||
if (!IS_DIGIT(grid[nx][ny])) continue;
|
||||
seen[nx][ny] = true;
|
||||
stack.push([nx, ny]);
|
||||
}
|
||||
}
|
||||
if (size >= 2) penalty += (size - 1) * 120;
|
||||
}
|
||||
|
||||
// dead-end-ish letter cell (3+ walls)
|
||||
const nbrs4 = [[-1, 0], [1, 0], [0, -1], [0, 1]];
|
||||
for (let r = 0; r < H; r++) for (let c = 0; c < W; c++) {
|
||||
if (!IS_LETTER_CELL(grid[r][c])) continue;
|
||||
let walls = 0;
|
||||
for (const [dr, dc] of nbrs4) {
|
||||
const rr = r + dr, cc = c + dc;
|
||||
if (rr < 0 || rr >= H || cc < 0 || cc >= W) {
|
||||
walls++;
|
||||
continue;
|
||||
}
|
||||
if (!IS_LETTER_CELL(grid[rr][cc])) walls++;
|
||||
}
|
||||
if (walls >= 3) penalty += 400;
|
||||
}
|
||||
|
||||
return penalty;
|
||||
}
|
||||
|
||||
/** --- Mask generation (memetic-ish + hillclimb) --- */
|
||||
function randomMask(rng) {
|
||||
const g = makeEmptyGrid();
|
||||
const targetClues = Math.round(W * H * 0.25); // ~18
|
||||
let placed = 0;
|
||||
let guard = 0;
|
||||
while (placed < targetClues && guard++ < 2000) {
|
||||
let placed = 0, guard = 0;
|
||||
|
||||
while (placed < targetClues && guard++ < 4000) {
|
||||
const r = rng.int(0, H - 1);
|
||||
const c = rng.int(0, W - 1);
|
||||
if (IS_DIGIT(g[r][c])) continue;
|
||||
|
||||
const d = String(rng.int(1, 4));
|
||||
g[r][c] = d;
|
||||
if (!hasRoomForClue(g, r, c, d)) {
|
||||
@@ -346,45 +312,33 @@ function randomMask(rng) {
|
||||
return g;
|
||||
}
|
||||
|
||||
/** Mutation: toggle a few cells with a "centralized" bias */
|
||||
function mutate(rng, grid) {
|
||||
const g = deepCopyGrid(grid);
|
||||
|
||||
// pick a center point to bias mutations (paper-style "centralized")
|
||||
const cx = rng.int(0, H - 1);
|
||||
const cy = rng.int(0, W - 1);
|
||||
|
||||
const steps = 4;
|
||||
for (let k = 0; k < steps; k++) {
|
||||
// gaussian-ish around (cx,cy) using sum of uniforms trick
|
||||
const rr = clamp(cx + (rng.int(-2, 2) + rng.int(-2, 2)), 0, H - 1);
|
||||
const cc = clamp(cy + (rng.int(-2, 2) + rng.int(-2, 2)), 0, W - 1);
|
||||
|
||||
const cur = g[rr][cc];
|
||||
if (IS_DIGIT(cur)) {
|
||||
// remove clue
|
||||
g[rr][cc] = "#";
|
||||
} else {
|
||||
// add clue with room
|
||||
const d = String(rng.int(1, 4));
|
||||
g[rr][cc] = d;
|
||||
if (!hasRoomForClue(g, rr, cc, d)) g[rr][cc] = "#";
|
||||
}
|
||||
}
|
||||
|
||||
return g;
|
||||
}
|
||||
|
||||
function clamp(x, a, b) { return Math.max(a, Math.min(b, x)); }
|
||||
|
||||
/** Angled crossover through center (closer to the paper than row-split) */
|
||||
function crossover(rng, a, b) {
|
||||
const out = makeEmptyGrid();
|
||||
const cx = (H - 1) / 2;
|
||||
const cy = (W - 1) / 2;
|
||||
const theta = rng.float() * Math.PI; // 0..pi
|
||||
|
||||
// line normal
|
||||
const theta = rng.float() * Math.PI;
|
||||
const nx = Math.cos(theta);
|
||||
const ny = Math.sin(theta);
|
||||
|
||||
@@ -394,24 +348,22 @@ function crossover(rng, a, b) {
|
||||
out[r][c] = (side >= 0) ? a[r][c] : b[r][c];
|
||||
}
|
||||
|
||||
// cleanup: if a clue has no room, turn it back into '#'
|
||||
// cleanup invalid clues
|
||||
for (let r = 0; r < H; r++) for (let c = 0; c < W; c++) {
|
||||
const ch = out[r][c];
|
||||
if (IS_DIGIT(ch) && !hasRoomForClue(out, r, c, ch)) out[r][c] = "#";
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Hillclimber (accept improving mutations) */
|
||||
function hillclimb(rng, start, dictIndex, limit) {
|
||||
function hillclimb(rng, start, lenCounts, limit) {
|
||||
let best = deepCopyGrid(start);
|
||||
let bestF = fitness(best, dictIndex);
|
||||
let bestF = maskFitness(best, lenCounts);
|
||||
let fails = 0;
|
||||
|
||||
while (fails < limit) {
|
||||
const cand = mutate(rng, best);
|
||||
const f = fitness(cand, dictIndex);
|
||||
const f = maskFitness(cand, lenCounts);
|
||||
if (f < bestF) {
|
||||
best = cand;
|
||||
bestF = f;
|
||||
@@ -423,7 +375,6 @@ function hillclimb(rng, start, dictIndex, limit) {
|
||||
return best;
|
||||
}
|
||||
|
||||
/** Similarity filter to avoid duplicates */
|
||||
function similarity(a, b) {
|
||||
let same = 0;
|
||||
for (let r = 0; r < H; r++) for (let c = 0; c < W; c++) {
|
||||
@@ -432,29 +383,26 @@ function similarity(a, b) {
|
||||
return same / (W * H);
|
||||
}
|
||||
|
||||
/** Memetic mask generator */
|
||||
function generateMask(rng, dictIndex, popSize, gens) {
|
||||
// init + strong hillclimb
|
||||
function generateMask(rng, lenCounts, popSize, gens) {
|
||||
console.log(`generateMask init pop: ${popSize}`);
|
||||
let pop = [];
|
||||
for (let i = 0; i < popSize; i++) {
|
||||
const g = randomMask(rng);
|
||||
pop.push(hillclimb(rng, g, dictIndex, 400)); // strong-ish
|
||||
pop.push(hillclimb(rng, g, lenCounts, 180)); // faster init
|
||||
}
|
||||
|
||||
for (let gen = 0; gen < gens; gen++) {
|
||||
const children = [];
|
||||
// create children from random pairs
|
||||
const pairs = Math.max(popSize, Math.floor(popSize * 1.5));
|
||||
for (let k = 0; k < pairs; k++) {
|
||||
const p1 = pop[rng.int(0, pop.length - 1)];
|
||||
const p2 = pop[rng.int(0, pop.length - 1)];
|
||||
const child = crossover(rng, p1, p2);
|
||||
children.push(hillclimb(rng, child, dictIndex, 120)); // weak repair
|
||||
children.push(hillclimb(rng, child, lenCounts, 70)); // light repair
|
||||
}
|
||||
|
||||
// merge + sort by fitness
|
||||
pop = pop.concat(children);
|
||||
pop.sort((x, y) => fitness(x, dictIndex) - fitness(y, dictIndex));
|
||||
pop.sort((x, y) => maskFitness(x, lenCounts) - maskFitness(y, lenCounts));
|
||||
|
||||
// similarity cull
|
||||
const next = [];
|
||||
@@ -470,31 +418,34 @@ function generateMask(rng, dictIndex, popSize, gens) {
|
||||
if (ok) next.push(cand);
|
||||
}
|
||||
pop = next;
|
||||
|
||||
if ((gen % 10) === 0) {
|
||||
const bestF = maskFitness(pop[0], lenCounts);
|
||||
console.log(` gen ${gen}/${gens} bestFitness=${bestF}`);
|
||||
}
|
||||
}
|
||||
|
||||
pop.sort((x, y) => fitness(x, dictIndex) - fitness(y, dictIndex));
|
||||
pop.sort((x, y) => maskFitness(x, lenCounts) - maskFitness(y, lenCounts));
|
||||
return pop[0];
|
||||
}
|
||||
|
||||
/** CSP fill: MRV + backtracking, using dictionary index */
|
||||
function fillMask(mask, dictIndex) {
|
||||
/** --- Fill (CSP) with NO huge candidate arrays --- */
|
||||
function fillMask(rng, mask, dictIndex, opts = {}) {
|
||||
const grid = deepCopyGrid(mask);
|
||||
const slots = extractSlots(grid)
|
||||
.filter(s => s.len >= MIN_LEN && s.len <= MAX_LEN);
|
||||
|
||||
// precompute slot directions for coverage sanity
|
||||
for (const s of slots) {
|
||||
const entry = dictIndex.get(s.len);
|
||||
if (!entry) return {ok: false, grid, clueMap: {}};
|
||||
}
|
||||
const slots = extractSlots(grid).filter(s => s.len >= MIN_LEN && s.len <= MAX_LEN);
|
||||
|
||||
const used = new Set();
|
||||
const assigned = new Map(); // slotKey -> word
|
||||
const assigned = new Map();
|
||||
|
||||
function slotKey(s) {
|
||||
const [r, c, d] = s.clue;
|
||||
return `${r},${c}:${d}`;
|
||||
}
|
||||
// progress options
|
||||
const logEveryMs = opts.logEveryMs ?? 250;
|
||||
const timeLimitMs = opts.timeLimitMs ?? 0; // 0 = no limit
|
||||
|
||||
// crossing weight precompute
|
||||
const cellCount = Array.from({length: H}, () => Array(W).fill(0));
|
||||
for (const s of slots) for (const [r, c] of s.cells) cellCount[r][c]++;
|
||||
|
||||
function slotKey(s) { return `${s.clue[0]},${s.clue[1]}:${s.clue[2]}`; }
|
||||
|
||||
function patternForSlot(s) {
|
||||
return s.cells.map(([r, c]) => {
|
||||
@@ -503,17 +454,10 @@ function fillMask(mask, dictIndex) {
|
||||
});
|
||||
}
|
||||
|
||||
function candidatesForSlot(s) {
|
||||
const entry = dictIndex.get(s.len);
|
||||
const pat = patternForSlot(s);
|
||||
const idxs = candidateIndicesForPattern(entry, pat);
|
||||
// map to words; filter repeats
|
||||
const out = [];
|
||||
for (const idx of idxs) {
|
||||
const w = entry.words[idx];
|
||||
if (!used.has(w)) out.push(w);
|
||||
}
|
||||
return out;
|
||||
function slotScore(s) {
|
||||
let cross = 0;
|
||||
for (const [r, c] of s.cells) cross += (cellCount[r][c] - 1);
|
||||
return cross * 10 + s.len;
|
||||
}
|
||||
|
||||
function placeWord(s, w) {
|
||||
@@ -526,47 +470,104 @@ function fillMask(mask, dictIndex) {
|
||||
undo.push([r, c, prev]);
|
||||
grid[r][c] = ch;
|
||||
} else if (prev !== ch) {
|
||||
return null; // conflict
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return undo;
|
||||
}
|
||||
|
||||
function undoPlace(undo) {
|
||||
for (const [r, c, prev] of undo) grid[r][c] = prev;
|
||||
function undoPlace(undo) { for (const [r, c, prev] of undo) grid[r][c] = prev; }
|
||||
|
||||
// ---- progress bar ----
|
||||
const t0 = Date.now();
|
||||
let lastLog = t0;
|
||||
let nodes = 0;
|
||||
let backtracks = 0;
|
||||
let lastMRV = 0;
|
||||
|
||||
function renderProgress(final = false) {
|
||||
const now = Date.now();
|
||||
if (!final && (now - lastLog) < logEveryMs) return;
|
||||
lastLog = now;
|
||||
|
||||
const done = assigned.size;
|
||||
const total = slots.length;
|
||||
const pct = total ? Math.floor((done / total) * 100) : 100;
|
||||
const barLen = 22;
|
||||
const filled = Math.min(barLen, Math.floor((pct / 100) * barLen));
|
||||
const bar = `[${"#".repeat(filled)}${"-".repeat(barLen - filled)}]`;
|
||||
|
||||
const elapsed = ((now - t0) / 1000).toFixed(1);
|
||||
const msg =
|
||||
`${bar} ${done}/${total} slots | nodes=${nodes} | backtracks=${backtracks} | mrv=${lastMRV} | ${elapsed}s`;
|
||||
|
||||
process.stdout.write("\r" + msg.padEnd(120));
|
||||
if (final) process.stdout.write("\n");
|
||||
}
|
||||
|
||||
function chooseMRV() {
|
||||
let best = null;
|
||||
let bestCands = null;
|
||||
let bestInfo = null;
|
||||
|
||||
for (const s of slots) {
|
||||
const k = slotKey(s);
|
||||
if (assigned.has(k)) continue;
|
||||
const cands = candidatesForSlot(s);
|
||||
if (cands.length === 0) return {slot: null, cands: null}; // dead end
|
||||
if (!best || cands.length < bestCands.length) {
|
||||
|
||||
const entry = dictIndex.get(s.len);
|
||||
if (!entry) return {slot: null, info: null};
|
||||
|
||||
const pat = patternForSlot(s);
|
||||
const info = candidateInfoForPattern(entry, pat);
|
||||
|
||||
if (info.count === 0) return {slot: null, info: null};
|
||||
|
||||
if (
|
||||
!best ||
|
||||
info.count < bestInfo.count ||
|
||||
(info.count === bestInfo.count && slotScore(s) > slotScore(best))
|
||||
) {
|
||||
best = s;
|
||||
bestCands = cands;
|
||||
if (cands.length === 1) break;
|
||||
bestInfo = info;
|
||||
if (info.count <= 1) break;
|
||||
}
|
||||
}
|
||||
return {slot: best, cands: bestCands};
|
||||
|
||||
if (!best) return {slot: null, info: {done: true}};
|
||||
return {slot: best, info: bestInfo};
|
||||
}
|
||||
|
||||
const MAX_TRIES_PER_SLOT = 500;
|
||||
|
||||
function backtrack() {
|
||||
const {slot, cands} = chooseMRV();
|
||||
if (slot === null && cands === null) return false;
|
||||
if (!slot) return true; // all assigned
|
||||
nodes++;
|
||||
|
||||
const k = slotKey(slot);
|
||||
if (timeLimitMs && (Date.now() - t0) > timeLimitMs) return false;
|
||||
|
||||
// simple LCV-ish: prefer words that introduce more crosses (more fixed letters)
|
||||
cands.sort((a, b) => scoreWord(slot, b) - scoreWord(slot, a));
|
||||
const pick = chooseMRV();
|
||||
if (!pick.slot && pick.info && pick.info.done) return true;
|
||||
if (!pick.slot) {
|
||||
backtracks++;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const w of cands) {
|
||||
const undo = placeWord(slot, w);
|
||||
if (!undo) continue;
|
||||
lastMRV = pick.info.count;
|
||||
renderProgress(false);
|
||||
|
||||
const s = pick.slot;
|
||||
const k = slotKey(s);
|
||||
const entry = dictIndex.get(s.len);
|
||||
const pat = patternForSlot(s);
|
||||
|
||||
const tryWord = (w) => {
|
||||
if (!w) return false;
|
||||
if (used.has(w)) return false;
|
||||
|
||||
for (let i = 0; i < pat.length; i++) {
|
||||
if (pat[i] && pat[i] !== w[i]) return false;
|
||||
}
|
||||
|
||||
const undo = placeWord(s, w);
|
||||
if (!undo) return false;
|
||||
|
||||
used.add(w);
|
||||
assigned.set(k, w);
|
||||
@@ -576,71 +577,100 @@ function fillMask(mask, dictIndex) {
|
||||
assigned.delete(k);
|
||||
used.delete(w);
|
||||
undoPlace(undo);
|
||||
return false;
|
||||
};
|
||||
|
||||
// constrained: iterate indices (bounded)
|
||||
if (pick.info.indices && pick.info.indices.length) {
|
||||
const idxs = pick.info.indices;
|
||||
const L = idxs.length;
|
||||
const tries = Math.min(MAX_TRIES_PER_SLOT, L);
|
||||
|
||||
// safe stepping even for L=1
|
||||
const start = (L === 1) ? 0 : rng.int(0, L - 1);
|
||||
const step = (L <= 1) ? 1 : rng.int(1, L - 1);
|
||||
|
||||
for (let t = 0; t < tries; t++) {
|
||||
const idx = idxs[(start + t * step) % L];
|
||||
const w = entry.words[idx];
|
||||
if (tryWord(w)) return true;
|
||||
}
|
||||
backtracks++;
|
||||
return false;
|
||||
}
|
||||
|
||||
// unconstrained: sample without building arrays
|
||||
const N = entry.words.length;
|
||||
if (N === 0) {
|
||||
backtracks++;
|
||||
return false;
|
||||
}
|
||||
|
||||
const tries = Math.min(MAX_TRIES_PER_SLOT, N);
|
||||
const start = (N === 1) ? 0 : rng.int(0, N - 1);
|
||||
const step = (N <= 1) ? 1 : rng.int(1, N - 1);
|
||||
|
||||
for (let t = 0; t < tries; t++) {
|
||||
const idx = (start + t * step) % N;
|
||||
const w = entry.words[idx];
|
||||
if (tryWord(w)) return true;
|
||||
}
|
||||
|
||||
backtracks++;
|
||||
return false;
|
||||
}
|
||||
|
||||
function scoreWord(slot, word) {
|
||||
// higher score: matches more already-fixed letters (less disruptive) and creates constraints
|
||||
let score = 0;
|
||||
for (let i = 0; i < slot.cells.length; i++) {
|
||||
const [r, c] = slot.cells[i];
|
||||
const prev = grid[r][c];
|
||||
if (prev !== "#" && prev === word[i]) score += 2;
|
||||
// prefer setting letters in cells that are crossed by another slot:
|
||||
// approximate by counting adjacent clue cells? keep cheap:
|
||||
score += 0.1;
|
||||
}
|
||||
return score;
|
||||
}
|
||||
|
||||
renderProgress(false);
|
||||
const ok = backtrack();
|
||||
renderProgress(true);
|
||||
|
||||
const clueMap = {};
|
||||
for (const [k, v] of assigned.entries()) clueMap[k] = v;
|
||||
|
||||
return {ok, grid, clueMap};
|
||||
return {ok, grid, clueMap, stats: {nodes, backtracks, seconds: (Date.now() - t0) / 1000}};
|
||||
}
|
||||
|
||||
/** Top-level: try generating mask + fill until success */
|
||||
/** --- Top-level: try mask+fill until success --- */
|
||||
function generatePuzzle(opts) {
|
||||
const rng = makeRng(opts.seed);
|
||||
console.time("LOAD_WORDS");
|
||||
const dict = loadWords(opts.wordsPath);
|
||||
console.timeEnd("LOAD_WORDS");
|
||||
|
||||
let best = null;
|
||||
for (let attempt = 1; attempt <= opts.tries; attempt++) {
|
||||
const mask = generateMask(rng, dict.index, opts.pop, opts.gens);
|
||||
const filled = fillMask(mask, dict.index);
|
||||
if (filled.ok) {
|
||||
best = {mask, filled};
|
||||
break;
|
||||
}
|
||||
console.log(`\nAttempt ${attempt}/${opts.tries}`);
|
||||
console.time("MASK");
|
||||
const mask = generateMask(rng, dict.lenCounts, opts.pop, opts.gens);
|
||||
console.timeEnd("MASK");
|
||||
|
||||
console.time("FILL");
|
||||
const filled = fillMask(rng, mask, dict.index, {logEveryMs: 200, timeLimitMs: 30000});
|
||||
console.timeEnd("FILL");
|
||||
|
||||
if (filled.ok) return {mask, filled};
|
||||
}
|
||||
return best;
|
||||
return null;
|
||||
}
|
||||
|
||||
// ---- main ----
|
||||
|
||||
(function main() {
|
||||
const opts = parseArgs(process.argv);
|
||||
const res = generatePuzzle(opts);
|
||||
const opts = parseArgs(process.argv);
|
||||
console.log(opts);
|
||||
|
||||
if (!res) {
|
||||
console.error("Failed to generate a fillable puzzle. Try increasing --tries/--gens, or provide a richer word-list.txt.");
|
||||
process.exit(1);
|
||||
}
|
||||
const res = generatePuzzle(opts);
|
||||
if (!res) {
|
||||
console.error("Failed to generate a fillable puzzle.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const maskStr = gridToString(res.mask);
|
||||
const filledStr = gridToString(res.filled.grid);
|
||||
// Existing logs...
|
||||
console.log("\n=== FILLED PUZZLE (RAW) ===");
|
||||
console.log(gridToString(res.filled.grid));
|
||||
|
||||
console.log("=== GENERATED MASK ===");
|
||||
console.log(maskStr);
|
||||
// ✅ Transform to your JSON format
|
||||
const puz = { grid: res.filled.grid, clueMap: res.filled.clueMap };
|
||||
const json = exportFormatFromFilled(puz, 1);
|
||||
|
||||
console.log("\n=== FILLED PUZZLE ===");
|
||||
console.log(filledStr);
|
||||
|
||||
console.log("\n=== CLUE -> WORD ===");
|
||||
// stable-ish ordering
|
||||
Object.keys(res.filled.clueMap)
|
||||
.sort((a, b) => a.localeCompare(b))
|
||||
.forEach(k => console.log(`${k} = ${res.filled.clueMap[k]}`));
|
||||
console.log("\n=== EXPORTED JSON ===");
|
||||
console.log(JSON.stringify(json, null, 2));
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user