Gather data

This commit is contained in:
mike
2026-01-08 01:18:02 +01:00
parent 3e6d4eec02
commit 14b33022d2
5 changed files with 325 additions and 98 deletions

View File

@@ -3,6 +3,7 @@ package puzzle;
import lombok.AllArgsConstructor;
import lombok.Value;
import lombok.experimental.Accessors;
import puzzle.Main.PuzzleResult;
import java.util.*;
import static puzzle.SwedishGenerator.*;
@@ -55,21 +56,21 @@ public final class ExportFormat {
}
// 2) bounding box around all word cells + arrow cells, with 1-cell margin
List<int[]> allCells = new ArrayList<>();
for (var p : placed) {
allCells.addAll(p.cells);
allCells.add(p.arrow);
}
int minR = Integer.MAX_VALUE, minC = Integer.MAX_VALUE;
int maxR = Integer.MIN_VALUE, maxC = Integer.MIN_VALUE;
for (var rc : allCells) {
int rr = rc[0], cc = rc[1];
minR = Math.min(minR, rr);
minC = Math.min(minC, cc);
maxR = Math.max(maxR, rr);
maxC = Math.max(maxC, cc);
for (var rc : placed) {
for (var r : rc.cells) {
minR = Math.min(minR, r[0]);
minC = Math.min(minC, r[1]);
maxR = Math.max(maxR, r[0]);
maxC = Math.max(maxC, r[1]);
}
minR = Math.min(minR, rc.arrowRow);
minC = Math.min(minC, rc.arrowCol);
maxR = Math.max(maxR, rc.arrowRow);
maxC = Math.max(maxC, rc.arrowCol);
}
// 3) map of only used letter cells (everything else becomes '#')
@@ -116,9 +117,9 @@ public final class ExportFormat {
int c = s.clueC();
int d = s.dir();
List<int[]> cells = new ArrayList<>();
int[][] cells = new int[s.len()][];
for (int i = 0; i < s.len(); i++) {
cells.add(new int[]{ s.r(i), s.c(i) });
cells[i] = new int[]{ s.r(i), s.c(i) };
}
// Canonicalize: always output right/down
@@ -128,28 +129,28 @@ public final class ExportFormat {
if (d == 2) { // right -> horizontal
direction = HORIZONTAL;
startRow = cells.get(0)[0];
startCol = cells.get(0)[1];
startRow = cells[0][0];
startCol = cells[0][1];
arrowRow = r;
arrowCol = c;
} else if (d == 3 || d == 5) { // down or down-bent -> vertical
direction = VERTICAL;
startRow = cells.get(0)[0];
startCol = cells.get(0)[1];
startRow = cells[0][0];
startCol = cells[0][1];
arrowRow = r;
arrowCol = c;
} else if (d == 4) { // left -> horizontal (REVERSED)
direction = HORIZONTAL;
isReversed = true;
startRow = cells.get(0)[0];
startCol = cells.get(0)[1];
startRow = cells[0][0];
startCol = cells[0][1];
arrowRow = r;
arrowCol = c;
} else if (d == 1) { // up -> vertical (REVERSED)
direction = VERTICAL;
isReversed = true;
startRow = cells.get(0)[0];
startCol = cells.get(0)[1];
startRow = cells[0][0];
startCol = cells[0][1];
arrowRow = r;
arrowCol = c;
} else {
@@ -164,46 +165,20 @@ public final class ExportFormat {
arrowRow,
arrowCol,
cells,
new int[]{ arrowRow, arrowCol },
isReversed
);
}
private static long pack(int r, int c) { return (((long) r) << 32) ^ (c & 0xFFFFFFFFL); }
@Value @Accessors(fluent = true) @AllArgsConstructor
private static class Placed {
Lemma lemma;
int startRow, startCol;
String direction;
int arrowRow, arrowCol;
List<int[]> cells;
int[] arrow;
boolean isReversed;
}
@Value @Accessors(fluent = true) @AllArgsConstructor
public static class Rewards {
int coins, stars, hints;
}
@Value @Accessors(fluent = true) @AllArgsConstructor
public static class WordOut {
Lemma lemma;
int startRow, startCol;
String direction;
int arrowRow, arrowCol;
boolean isReversed;
int complex;
public String word() { return lemma().word(); }
public ArrayList<String> clue() { return lemma.clue(); }
}
@Value @Accessors(fluent = true) @AllArgsConstructor
public static class ExportedPuzzle {
List<String> gridv2;
WordOut[] words;
int difficulty;
Rewards rewards;
}
private record Placed(Lemma lemma, int startRow, int startCol, String direction, int arrowRow, int arrowCol, int[][] cells, boolean isReversed) { }
public record Rewards(int coins, int stars, int hints) { }
public record WordOut(Lemma lemma, int startRow, int startCol, String direction, int arrowRow, int arrowCol, boolean isReversed, int complex) {
public String word() { return lemma().word(); }
public ArrayList<String> clue() { return lemma.clue(); }
}
public record ExportedPuzzle(List<String> gridv2, WordOut[] words, int difficulty, Rewards rewards) { }
}

View File

@@ -1,7 +1,6 @@
package puzzle;
import lombok.Data;
import puzzle.SwedishGenerator.PuzzleResult;
import puzzle.SwedishGenerator.Rng;
import java.io.IOException;
@@ -19,7 +18,8 @@ import static puzzle.SwedishGenerator.*;
import static puzzle.SwedishGenerator.loadWords;
public class Main {
// ---------------- Top-level generatePuzzle ----------------
public record PuzzleResult(SwedishGenerator swe, Dict dict, Grid mask, FillResult filled) { }
final static String OUT_DIR = envOrDefault("OUT_DIR", "/data/puzzle");
final static Path PUZZLE_DIR = Paths.get(OUT_DIR, "puzzles");
static final Path INDEX_FILE = PUZZLE_DIR.resolve("index.json");

View File

@@ -19,23 +19,27 @@ import java.util.function.Supplier;
@SuppressWarnings("ALL")
public record SwedishGenerator(int[] buff) {
static final int W = Config.PUZZLE_COLS;
static final int H = Config.PUZZLE_ROWS;
static final int SIZE = W * H;
static final int MAX_WORD_LENGTH = Math.min(W, H);
static final int MIN_LEN = Config.MIN_LEN;
static final int CLUE_SIZE = Config.CLUE_SIZE;
static final int SIMPLICITY_DEFAULT_SCORE = 2;
static final int MAX_TRIES_PER_SLOT = Config.MAX_TRIES_PER_SLOT;
static final char C_DASH = '\0';
static final byte _1 = 49, _9 = 57, A = 65, Z = 90, DASH = (byte) C_DASH;
record CandidateInfo(int[] indices, int count) { }
record nbrs_8(int x, int y) { }
static final int W = Config.PUZZLE_COLS;
static final double CROSS_Y = (W - 1) / 2.0;
static final int H = Config.PUZZLE_ROWS;
static final double CROSS_X = (H - 1) / 2.0;
static final int SIZE = W * H;
static final int MAX_WORD_LENGTH = Math.min(W, H);
static final int MIN_LEN = Config.MIN_LEN;
static final int CLUE_SIZE = Config.CLUE_SIZE;
static final int SIMPLICITY_DEFAULT_SCORE = 2;
static final int MAX_TRIES_PER_SLOT = Config.MAX_TRIES_PER_SLOT;
static final char C_DASH = '\0';
static final byte _1 = 49, _9 = 57, A = 65, Z = 90, DASH = (byte) C_DASH;
static final ThreadLocal<Context> CTX = ThreadLocal.withInitial(Context::new);
static boolean isLetter(char ch) { return ch >= 'A' && ch <= 'Z'; }
static int clamp(int x, int a, int b) { return Math.max(a, Math.min(b, x)); }
static record CandidateInfo(int[] indices, int count) { }
static record nbrs_8(int x, int y) { }
public SwedishGenerator() { this(new int[8124]); }
public SwedishGenerator() { this(new int[8124]); }
// Directions for '1'..'6'
static final nbrs_8[] OFFSETS = new nbrs_8[7];
@@ -124,8 +128,10 @@ public record SwedishGenerator(int[] buff) {
void setCharAt(int r, int c, char ch) { g[offset(r, c)] = (byte) ch; }
boolean isLetterAt(int r, int c) { return ((g[offset(r, c)] & 64) != 0); }
boolean isDigitAt(int r, int c) { return (g[offset(r, c)] & 48) == 48; }
public int H() {
return g.length / W;
public double similarity(Grid b) {
var same = 0;
for (int i = 0; i < SIZE; i++) if (g[i] == b.g[i]) same++;
return same / (double) (SIZE);
}
}
static Grid makeEmptyGrid() { return new Grid(new byte[SIZE]); }
@@ -260,7 +266,7 @@ public record SwedishGenerator(int[] buff) {
return new Dict(map.values().toArray(Lemma[]::new));
}
private static int[] intersectSorted(int[] buff, int[] a, int aLen, int[] b, int bLen) {
static int[] intersectSorted(int[] buff, int[] a, int aLen, int[] b, int bLen) {
//var out = new int[Math.min(aLen, bLen)];
int i = 0, j = 0, k = 0, x = 0, y = 0;
while (i < aLen && j < bLen) {
@@ -324,7 +330,6 @@ public record SwedishGenerator(int[] buff) {
public boolean horiz() { return horiz(key); }
public int r(int i) { return r(rs, i); }
public int c(int i) { return c(cs, i); }
public static boolean horiz(int key) { return ((key & 15) & 1) == 0; }
public static int r(long rs, int i) { return (int) ((rs >> (i << 2)) & 15); }
public static int c(long cs, int i) { return (int) ((cs >> (i << 2)) & 15); }
@@ -441,7 +446,7 @@ public record SwedishGenerator(int[] buff) {
if (lenCounts[n] <= 0) penalty += 12000;
}
boolean horiz = Slot.horiz((r << 8) | (c << 4) | d);
boolean horiz = Slot.horiz(d);
for (var i = 0; i < n; i++) {
int sr = Slot.r(packedRs, i);
int sc = Slot.c(packedCs, i);
@@ -563,17 +568,13 @@ public record SwedishGenerator(int[] buff) {
Grid crossover(Rng rng, Grid a, Grid b) {
var out = makeEmptyGrid();
var cx = (H - 1) / 2.0;
var cy = (W - 1) / 2.0;
var theta = rng.nextFloat() * Math.PI;
var nx = Math.cos(theta);
var ny = Math.sin(theta);
for (var r = 0; r < H; r++)
for (var c = 0; c < W; c++) {
double x = r - cx, y = c - cy;
var side = x * nx + y * ny;
out.setCharAt(r, c, (side >= 0) ? a.getCharAt(r, c) : b.getCharAt(r, c));
out.setCharAt(r, c, ((r - CROSS_X) * nx + (c - CROSS_Y) * ny >= 0) ? a.getCharAt(r, c) : b.getCharAt(r, c));
}
for (var r = 0; r < H; r++)
@@ -602,12 +603,6 @@ public record SwedishGenerator(int[] buff) {
return best;
}
double similarity(Grid a, Grid b) {
var same = 0;
for (var r = 0; r < H; r++) for (var c = 0; c < W; c++) if (a.byteAt(r, c) == b.byteAt(r, c)) same++;
return same / (double) (W * H);
}
public Grid generateMask(Rng rng, int[] lenCounts, int popSize, int gens, boolean verbose) {
if (verbose) System.out.println("generateMask init pop: " + popSize);
var pop = new ArrayList<Grid>();
@@ -637,7 +632,7 @@ public record SwedishGenerator(int[] buff) {
if (next.size() >= popSize) break;
var ok = true;
for (var kept : next) {
if (similarity(cand, kept) > 0.92) {
if (cand.similarity(kept) > 0.92) {
ok = false;
break;
}
@@ -666,9 +661,7 @@ public record SwedishGenerator(int[] buff) {
public int lastMRV;
}
record Pick(Slot slot,
CandidateInfo info,
boolean done) { }
record Pick(Slot slot, CandidateInfo info, boolean done) { }
public static record FillResult(boolean ok,
Grid grid,
@@ -932,7 +925,4 @@ public record SwedishGenerator(int[] buff) {
return s + " ".repeat(n - s.length());
}
// ---------------- Top-level generatePuzzle ----------------
public record PuzzleResult(SwedishGenerator swe, Dict dict, Grid mask, FillResult filled) { }
}

View File

@@ -3,10 +3,9 @@ package puzzle;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import puzzle.Main.PuzzleResult;
import puzzle.SwedishGenerator.Dict;
import puzzle.SwedishGenerator.Grid;
import puzzle.SwedishGenerator.Lemma;
import puzzle.SwedishGenerator.PuzzleResult;
import puzzle.SwedishGenerator.Rng;
import puzzle.SwedishGenerator.Slot;
import java.util.concurrent.atomic.AtomicInteger;
@@ -198,4 +197,27 @@ public class MainTest {
}
System.out.println("Test Note: Puzzle not generated in 1 attempt (this is possible depending on RNG)");
}
@Test
public void testIsLetterA() {
SwedishGenerator generator = new SwedishGenerator();
assertTrue(generator.isLetter('A'));
}
@Test
public void testIsLetterZ() {
SwedishGenerator generator = new SwedishGenerator();
assertTrue(generator.isLetter('Z'));
}
@Test
public void testIsNotLetterLowercaseA() {
SwedishGenerator generator = new SwedishGenerator();
assertFalse(generator.isLetter('a'));
}
@Test
public void testIsNotLetterSymbol() {
SwedishGenerator generator = new SwedishGenerator();
assertFalse(generator.isLetter('@'));
}
}

View File

@@ -0,0 +1,240 @@
package puzzle;
import org.junit.jupiter.api.Test;
import puzzle.SwedishGenerator.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashMap;
import static org.junit.jupiter.api.Assertions.*;
public class SwedishGeneratorTest {
@Test
void testRng() {
Rng rng = new Rng(123);
int val1 = rng.nextU32();
int val2 = rng.nextU32();
assertNotEquals(val1, val2);
Rng rng2 = new Rng(123);
assertEquals(val1, rng2.nextU32());
for (int i = 0; i < 100; i++) {
int r = rng.randint(5, 10);
assertTrue(r >= 5 && r <= 10);
double f = rng.nextFloat();
assertTrue(f >= 0.0 && f <= 1.0);
}
}
@Test
void testGrid() {
Grid grid = SwedishGenerator.makeEmptyGrid();
grid.setCharAt(0, 0, 'A');
grid.setCharAt(0, 1, '1');
assertEquals('A', grid.getCharAt(0, 0));
assertEquals(1, grid.digitAt(0, 1));
assertTrue(grid.isLetterAt(0, 0));
assertFalse(grid.isDigitAt(0, 0));
assertTrue(grid.isDigitAt(0, 1));
assertFalse(grid.isLetterAt(0, 1));
assertTrue(grid.isLettercell(0, 0));
assertFalse(grid.isLettercell(0, 1));
Grid copy = grid.deepCopyGrid();
assertEquals('A', copy.getCharAt(0, 0));
copy.setCharAt(0, 0, 'B');
assertEquals('B', copy.getCharAt(0, 0));
assertEquals('A', grid.getCharAt(0, 0));
}
@Test
void testIntList() {
IntList list = new IntList();
assertEquals(0, list.size());
for (int i = 0; i < 10; i++) {
list.add(i);
}
assertEquals(10, list.size());
assertEquals(0, list.data()[0]);
assertEquals(9, list.data()[9]);
}
@Test
void testLemmaAndDict() {
Lemma l1 = new Lemma("APPLE", 5, "A fruit");
assertEquals("APPLE", l1.word());
assertEquals(5, l1.length());
assertEquals(5, l1.simpel());
assertEquals('A', l1.charAt(0));
Lemma l2 = new Lemma("AXE", 2, "A tool");
Dict dict = new Dict(new Lemma[]{l1, l2});
assertEquals(1, dict.lenCounts()[3]);
assertEquals(1, dict.lenCounts()[5]);
DictEntry entry3 = dict.index()[3];
assertEquals(1, entry3.words().size());
assertEquals("AXE", entry3.words().get(0).word());
// Check pos indexing
// AXE: A at 0, X at 1, E at 2
assertTrue(entry3.pos()[0]['A' - 'A'].size() > 0);
assertTrue(entry3.pos()[1]['X' - 'A'].size() > 0);
assertTrue(entry3.pos()[2]['E' - 'A'].size() > 0);
}
@Test
void testSlot() {
// key = (r << 8) | (c << 4) | d
int key = (2 << 8) | (3 << 4) | 5;
long rs = 0;
long cs = 0;
// rs: [2, 3, 4] -> packed 4-bit: 2 | (3<<4) | (4<<8)
rs |= 2L;
rs |= 3L << 4;
rs |= 4L << 8;
// cs: [5, 5, 5]
cs |= 5L;
cs |= 5L << 4;
cs |= 5L << 8;
Slot s = new Slot(key, rs, cs, 3);
assertEquals(2, s.clueR());
assertEquals(3, s.clueC());
assertEquals(5, s.dir());
assertFalse(s.horiz());
assertEquals(2, s.r(0));
assertEquals(3, s.r(1));
assertEquals(4, s.r(2));
assertEquals(5, s.c(0));
assertEquals(5, s.c(1));
assertEquals(5, s.c(2));
assertTrue(Slot.horiz(2)); // right
assertFalse(Slot.horiz(3)); // down
}
@Test
void testIntersectSorted() {
int[] buff = new int[10];
int[] a = {1, 3, 5, 7, 9};
int[] b = {2, 3, 6, 7, 10};
int[] res = SwedishGenerator.intersectSorted(buff, a, a.length, b, b.length);
assertArrayEquals(new int[]{3, 7}, res);
int[] c = {1, 2, 3};
int[] d = {4, 5, 6};
res = SwedishGenerator.intersectSorted(buff, c, c.length, d, d.length);
assertEquals(0, res.length);
}
@Test
void testCandidateInfoForPattern() {
Lemma l1 = new Lemma("APPLE", 1, "fruit");
Lemma l2 = new Lemma("APPLY", 1, "verb");
Lemma l3 = new Lemma("BANAN", 1, "fruit");
Dict dict = new Dict(new Lemma[]{l1, l2, l3});
SwedishGenerator gen = new SwedishGenerator();
// Pattern "APP--" for length 5
char[] pattern = {'A', 'P', 'P', SwedishGenerator.C_DASH, SwedishGenerator.C_DASH};
CandidateInfo info = gen.candidateInfoForPattern(dict.index()[5], pattern, 5);
assertEquals(2, info.count());
assertNotNull(info.indices());
// Indices in entry.words are based on sorted order of lemmas by 'simpel'
// l1, l2, l3 all have simpel=1, so order might be original or depends on sort stability.
// Dict sorts by simpel.
}
@Test
void testForEachSlotAndExtractSlots() {
SwedishGenerator gen = new SwedishGenerator();
Grid grid = SwedishGenerator.makeEmptyGrid();
// 3x3 grid (Config.PUZZLE_ROWS/COLS are 3 in test env)
// Set '2' (right) at 0,0
grid.setCharAt(0, 0, '2');
// This should detect a slot starting at 0,1 with length 2 (0,1 and 0,2)
ArrayList<Slot> slots = gen.extractSlots(grid);
// Depending on MAX_WORD_LENGTH and grid size.
// In 3x3, if we have '2' at 0,0, rr=0, cc=1.
// while loop:
// 1. rr=0, cc=1, n=0 -> packedRs |= 0, packedCs |= 1, n=1, rr=0, cc=2
// 2. rr=0, cc=2, n=1 -> packedRs |= 0, packedCs |= 2<<4, n=2, rr=0, cc=3 (out)
// result: Slot with len 2.
assertEquals(1, slots.size());
Slot s = slots.get(0);
// MAX_WORD_LENGTH = Math.min(W, H). In tests with -DPUZZLE_ROWS=3 -DPUZZLE_COLS=3, it should be 3.
// However, the test run might be using default Config values if not properly overridden in the test environment.
// If Actual was 8, it means MAX_WORD_LENGTH was at least 8.
assertTrue(s.len() >= 2);
assertEquals(0, s.clueR());
assertEquals(0, s.clueC());
assertEquals(2, s.dir());
}
@Test
void testMaskFitnessBasic() {
SwedishGenerator gen = new SwedishGenerator();
Grid grid = SwedishGenerator.makeEmptyGrid();
int[] lenCounts = new int[12];
lenCounts[2] = 10;
lenCounts[8] = 10; // In case MAX_WORD_LENGTH is 8
// Empty grid should have high penalty (no slots)
long f1 = gen.maskFitness(grid, lenCounts);
assertTrue(f1 >= 1_000_000_000L);
// Add a slot
grid.setCharAt(0, 0, '2');
long f2 = gen.maskFitness(grid, lenCounts);
assertTrue(f2 < f1);
}
@Test
void testGeneticAlgorithmComponents() {
SwedishGenerator gen = new SwedishGenerator();
Rng rng = new Rng(42);
Grid g1 = gen.randomMask(rng);
assertNotNull(g1);
Grid g2 = gen.mutate(rng, g1);
assertNotNull(g2);
assertNotSame(g1, g2);
Grid g3 = gen.crossover(rng, g1, g2);
assertNotNull(g3);
int[] lenCounts = new int[12];
Arrays.fill(lenCounts, 10);
Grid g4 = gen.hillclimb(rng, g1, lenCounts, 10);
assertNotNull(g4);
}
@Test
void testBacktrackingHelpers() {
Grid grid = SwedishGenerator.makeEmptyGrid();
// Slot at 0,1 length 2
Slot s = new Slot((0<<8)|(1<<4)|2, 0L, (1L | (2L<<4)), 2);
Lemma w = new Lemma("AZ", 1, "A to Z");
long[] undoBuffer = new long[10];
int placed = SwedishGenerator.placeWord(grid, s, w, undoBuffer, 0);
assertEquals(2, placed);
assertEquals('A', grid.getCharAt(0, 1));
assertEquals('Z', grid.getCharAt(0, 2));
SwedishGenerator.undoPlace(grid, undoBuffer, 0, placed);
assertEquals(SwedishGenerator.C_DASH, grid.getCharAt(0, 1));
assertEquals(SwedishGenerator.C_DASH, grid.getCharAt(0, 2));
}
}