introduce bitloops

This commit is contained in:
mike
2026-01-19 16:31:33 +01:00
parent 37581d15b4
commit 1fa112ab65
14 changed files with 393 additions and 455 deletions

View File

@@ -18,17 +18,17 @@ public class ConnectivityTest {
// Clue 1: (0,0) Right. Slot: (0,1), (0,2), (0,3)
// Clue 2: (1,2) Up. Slot: (0,2)
// Ze zijn NIET 8-naburig, maar wel verbonden via slot op (0,2)
singleComp.setClueLo(1L << SwedishGenerator.Grid.offset(0,0), (byte)1);
singleComp.setClueLo(1L << SwedishGenerator.Grid.offset(2,2), (byte)2); // Up van (2,2) naar (1,2), (0,2)
singleComp.setClueLo(1L << Masker.offset(0, 0), (byte)1);
singleComp.setClueLo(1L << Masker.offset(2, 2), (byte)2); // Up van (2,2) naar (1,2), (0,2)
long fitnessSingle = masker.maskFitness(singleComp, 2);
// 2. Maak een masker met twee eilandjes van clues
Clues twoIslands = Clues.createEmpty();
twoIslands.setClueLo(1L << SwedishGenerator.Grid.offset(0,0), (byte)1);
twoIslands.setClueLo( SwedishGenerator.Grid.offset(7,7) < 64 ? 1L << SwedishGenerator.Grid.offset(7,7) : 0, (byte)1);
twoIslands.setClueLo(1L << Masker.offset(0, 0), (byte)1);
twoIslands.setClueLo(Masker.offset(7, 7) < 64 ? 1L << Masker.offset(7, 7) : 0, (byte)1);
// Voor de zekerheid checken we of offset(7,7) in lo of hi zit
int off77 = SwedishGenerator.Grid.offset(7,7);
int off77 = Masker.offset(7, 7);
if (off77 < 64) twoIslands.setClueLo(1L << off77, (byte)1);
else twoIslands.setClueHi(1L << (off77 - 64), (byte)1);
@@ -49,8 +49,8 @@ public class ConnectivityTest {
// Twee clues naast elkaar, maar slots kruisen niet.
// Clue 1: (1,1) Right. Slot (1,2), (1,3), (1,4)
// Clue 2: (2,1) Right. Slot (2,2), (2,3), (2,4)
clues.setClueLo(1L << SwedishGenerator.Grid.offset(1,1), (byte)1);
clues.setClueLo(1L << SwedishGenerator.Grid.offset(2,1), (byte)1);
clues.setClueLo(1L << Masker.offset(1, 1), (byte)1);
clues.setClueLo(1L << Masker.offset(2, 1), (byte)1);
long fitness = masker.maskFitness(clues, 2);
@@ -68,8 +68,8 @@ public class ConnectivityTest {
// Clue A: (2,0) Right. Slot: (2,1), (2,2), (2,3), ...
// Clue B: (1,2) Corner Down. Word starts at (1,3) en gaat omlaag: (1,3), (2,3), (3,3)...
// Ze kruisen op (2,3).
clues.setClueLo(1L << SwedishGenerator.Grid.offset(2,0), (byte)1); // Right
clues.setClueLo(1L << SwedishGenerator.Grid.offset(1,2), (byte)4); // Corner Down
clues.setClueLo(1L << Masker.offset(2, 0), (byte)1); // Right
clues.setClueLo(1L << Masker.offset(1, 2), (byte)4); // Corner Down
long fitness = masker.maskFitness(clues, 2);
System.out.println("[DEBUG_LOG] Fitness corner clue connected: " + fitness);
@@ -77,11 +77,11 @@ public class ConnectivityTest {
// Als ze verbonden zijn, is de penalty voor eilandjes 0.
// We vergelijken met een island scenario (2 clues die elkaar NIET raken)
Clues island = Clues.createEmpty();
int offA = SwedishGenerator.Grid.offset(2,0);
int offA = Masker.offset(2, 0);
if (offA < 64) island.setClueLo(1L << offA, (byte)1);
else island.setClueHi(1L << (offA - 64), (byte)1);
int offB = SwedishGenerator.Grid.offset(7,7);
int offB = Masker.offset(7, 7);
if (offB < 64) island.setClueLo(1L << offB, (byte)1);
else island.setClueHi(1L << (offB - 64), (byte)1);

View File

@@ -11,7 +11,7 @@ public class CornerClueTest {
void testCornerDownSlot() {
Clues clues = Clues.createEmpty();
// Clue op (0,0), type 4 (Corner Down)
int idx = SwedishGenerator.Grid.offset(0,0);
int idx = Masker.offset(0, 0);
clues.setClueLo(1L << idx, (byte)4);
assertEquals(4, clues.getDir(idx));
@@ -22,10 +22,10 @@ public class CornerClueTest {
if (Masker.Slot.dir(key) == 4) {
found[0] = true;
// Woord zou moeten starten op (0,1)
int startIdx = SwedishGenerator.Grid.offset(0, 1);
int startIdx = Masker.offset(0, 1);
assertTrue((lo & (1L << startIdx)) != 0, "Slot should start at (0,1)");
// En omlaag gaan
int secondIdx = SwedishGenerator.Grid.offset(1, 1);
int secondIdx = Masker.offset(1, 1);
assertTrue((lo & (1L << secondIdx)) != 0, "Slot should continue to (1,1)");
// Lengte van het slot zou 8 moeten zijn (van rij 0 t/m 7 in kolom 1)
@@ -38,7 +38,7 @@ public class CornerClueTest {
@Test
void testCornerDownExtraction() {
Clues clues = Clues.createEmpty();
int idx = SwedishGenerator.Grid.offset(0,0);
int idx = Masker.offset(0, 0);
clues.setClueLo(1L << idx, (byte)4);
DictEntry[] dict = DictData.DICT.index();

View File

@@ -46,7 +46,7 @@ public final class DictJavaGeneratorMulti {
try (var lines = Files.lines(wordsPath, StandardCharsets.UTF_8)) {
lines.forEach(line -> {
CsvIndexService.lineToLemma(line, w -> {
String word = Lemma.asWord(w);
String word = Lemma.asWord(w, Export.BYTES.get());
String[] clues = CsvIndexService.lineToClue(line);
int simpel = CsvIndexService.lineToSimpel(line);

View File

@@ -1,6 +1,5 @@
package puzzle;
import lombok.AllArgsConstructor;
import lombok.val;
import org.junit.jupiter.api.Test;
import puzzle.Export.Clued;
@@ -13,13 +12,11 @@ import puzzle.SwedishGenerator.FillResult;
import puzzle.SwedishGenerator.Lemma;
import puzzle.SwedishGenerator.Slotinfo;
import puzzle.SwedishGeneratorTest.Idx;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static puzzle.ExportFormatTest.Clue.RIGHT;
import static puzzle.Export.Clue.LEFT;
import static puzzle.Export.Clue.RIGHT;
import static puzzle.SwedishGenerator.C;
import static puzzle.Masker.Clues;
import static puzzle.SwedishGenerator.FillStats;
@@ -34,25 +31,6 @@ import static puzzle.SwedishGeneratorTest.TEST;
public class ExportFormatTest {
static final byte CLUE_DOWN = 0;
static final byte CLUE_RIGHT = 1;
static final byte CLUE_UP = 2;
static final byte CLUE_LEFT = 3;
@AllArgsConstructor
enum Clue {
DOWN(CLUE_DOWN),
RIGHT(CLUE_RIGHT),
UP(CLUE_UP),
LEFT(CLUE_LEFT);
Clue(byte dir) {
this.dir = dir;
this.clueDir = dir;
}
final byte dir;
final int clueDir;
}
@Test
void testExportFormatFromFilled() {
val clues = Clues.createEmpty();
@@ -60,11 +38,11 @@ public class ExportFormatTest {
clues.setClueLo(Idx.IDX_0_0.lo, RIGHT.dir);
// This creates a slot starting at (0,1)
// Terminate the slot at (0,5) with another digit to avoid it extending to MAX_WORD_LENGTH
clues.setClueLo(Idx.IDX_0_5.lo, CLUE_LEFT);
var grid = new Gridded(clues.toGrid());
clues.setClueLo(Idx.IDX_0_5.lo, LEFT.dir);
var grid = new Gridded(clues);
// key = (cellIndex << 2) | (direction)
var key = Slot.packSlotKey(0, CLUE_RIGHT);
var key = Slot.packSlotKey(0, RIGHT.dir);
var lo = (1L << OFF_0_1) | (1L << OFF_0_2) | (1L << OFF_0_3) | (1L << OFF_0_4);
assertTrue(placeWord(grid.grid(), grid.grid().g, key, lo, 0L, TEST));
@@ -112,7 +90,7 @@ public class ExportFormatTest {
var grid = SwedishGeneratorTest.createEmpty();
val clues = Clues.createEmpty();
var fillResult = new FillResult(true, 0, 0, 0, 0, new FillStats());
var puzzleResult = new PuzzleResult(new Clued(clues), new Gridded(grid), new Slotinfo[0], fillResult);
var puzzleResult = new PuzzleResult(new Clued(clues), new Gridded(grid, clues), new Slotinfo[0], fillResult);
var exported = puzzleResult.exportFormatFromFilled(1, new Rewards(0, 0, 0));
@@ -133,13 +111,13 @@ public class ExportFormatTest {
val words = entry.words();
for (int i = 0; i < Math.min(words.length, 5); i++) {
val wordVal = words[i];
val word = Lemma.asWord(wordVal);
val word = Lemma.asWord(wordVal, Export.BYTES.get());
val assigned = new Assign(wordVal);
val shard = Meta.shardKey(assigned.w);
val clueRec = Meta.readRecord(shard, i);
assertNotNull(clueRec);
assertEquals(word, Lemma.asWord(clueRec.w()));
assertEquals(word, Lemma.asWord(clueRec.w(), Export.BYTES.get()));
assertTrue(clueRec.simpel() >= 0);
assertTrue(clueRec.clues().length > 0);
}
@@ -160,7 +138,7 @@ public class ExportFormatTest {
int idx = -1;
long[] words = entry.words();
for (int i = 0; i < words.length; i++) {
if (Lemma.asWord(words[i]).equals(wStr)) {
if (Lemma.asWord(words[i], Export.BYTES.get()).equals(wStr)) {
idx = i;
break;
}
@@ -170,7 +148,7 @@ public class ExportFormatTest {
val shard = Meta.shardKey(w);
val clueRec = Meta.readRecord(shard, idx);
assertNotNull(clueRec);
assertEquals(wStr, Lemma.asWord(clueRec.w()));
assertEquals(wStr, Lemma.asWord(clueRec.w(), Export.BYTES.get()));
// Check some expected complexity values (from CSV head output, column 3)
if (wStr.equals("EEN")) {
assertEquals(451, clueRec.simpel());

View File

@@ -10,20 +10,18 @@ import puzzle.Export.LetterVisit.LetterAt;
import puzzle.Export.PuzzleResult;
import puzzle.Export.Rewards;
import puzzle.Main.Opts;
import puzzle.Masker.Clues;
import puzzle.SwedishGenerator.Rng;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static puzzle.ExportFormatTest.Clue.DOWN;
import static puzzle.ExportFormatTest.Clue.LEFT;
import static puzzle.ExportFormatTest.Clue.RIGHT;
import static puzzle.ExportFormatTest.Clue.UP;
import static puzzle.Export.Clue.DOWN;
import static puzzle.Export.Clue.LEFT;
import static puzzle.Export.Clue.RIGHT;
import static puzzle.Export.Clue.UP;
import static puzzle.SwedishGenerator.Dict;
import static puzzle.SwedishGenerator.Lemma;
import static puzzle.SwedishGenerator.STACK_SIZE;
import static puzzle.SwedishGenerator.Slotinfo;
import static puzzle.SwedishGenerator.fillMask;
import static puzzle.SwedishGeneratorTest.AB;
@@ -66,7 +64,7 @@ public class MainTest {
var clues = Masker.Clues.createEmpty();
val key = Masker.Slot.packSlotKey(OFF_0_0, CLUE_RIGHT);
clues.setClueLo(IDX_0_0.lo, CLUE_RIGHT);
var grid = new Gridded(clues.toGrid());
var grid = new Gridded(clues);
val g = grid.grid().g;
GridBuilder.placeWord(grid.grid(), g, key, (1L << OFF_0_1) | (1L << OFF_0_2), 0, AB);
@@ -74,11 +72,11 @@ public class MainTest {
assertEquals(1, slots.length);
var s = slots[0];
assertEquals(8, Masker.Slot.length(s.lo(), s.hi()));
var cells = s.walk().toArray();
assertEquals(0, SwedishGenerator.IT[cells[0]].r());
assertEquals(1, SwedishGenerator.IT[cells[0]].c());
assertEquals(0, SwedishGenerator.IT[cells[1]].r());
assertEquals(2, SwedishGenerator.IT[cells[1]].c());
var cells = Gridded.cellWalk((byte) s.key(), s.lo(), s.hi()).toArray();
assertEquals(0, Masker.IT[cells[0]].r());
assertEquals(1, Masker.IT[cells[0]].c());
assertEquals(0, Masker.IT[cells[1]].r());
assertEquals(2, Masker.IT[cells[1]].c());
}
@Test
@@ -98,8 +96,8 @@ public class MainTest {
clues.forEachSlot((key, lo, hi) -> {
count.incrementAndGet();
assertEquals(8, Long.bitCount(lo) + Long.bitCount(hi));
assertEquals(0, SwedishGenerator.IT[Long.numberOfTrailingZeros(lo)].r());
assertEquals(1, SwedishGenerator.IT[Long.numberOfTrailingZeros(lo)].c());
assertEquals(0, Masker.IT[Long.numberOfTrailingZeros(lo)].r());
assertEquals(1, Masker.IT[Long.numberOfTrailingZeros(lo)].c());
});
assertEquals(1, count.get());
}
@@ -115,7 +113,7 @@ public class MainTest {
var clues = new Clued(Masker.Clues.createEmpty());
val key = Masker.Slot.packSlotKey(OFF_2_1, CLUE_UP);
clues.setClueLo(IDX_2_1.lo, CLUE_UP);
var grid = new Gridded(clues.toGrid());
var grid = new Gridded(clues.c());
// Test set/get
GridBuilder.placeWord(grid.grid(), grid.grid().g, key, (1L << OFF_1_1) | (1L << OFF_0_1), 0, AZ);
@@ -184,7 +182,7 @@ public class MainTest {
@Test
void testFiller2() {
val rng = new Rng(-343913721);
val mask = Clues.parse(
val mask = Clued.parse(
"1 000000\n" +
"1 \n" +
"1 \n" +
@@ -203,7 +201,7 @@ public class MainTest {
@Test
void testFiller() {
val rng = new Rng(-343913721);
val mask = Clues.parse(
val mask = Clued.parse(
" 3 300\n" +
" 1 \n" +
" 1 \n" +
@@ -214,13 +212,13 @@ public class MainTest {
"21 22 3");
var slotInfo = Masker.slots(mask.c(), dict.index());
var grid = Slotinfo.grid(slotInfo);
var filled = fillMask(rng, slotInfo, grid, false);
var filled = fillMask(rng, slotInfo, grid);
Assertions.assertTrue(filled.ok(), "Puzzle generation failed (not ok)");
Assertions.assertEquals(13, Slotinfo.wordCount(0, slotInfo), "Number of assigned words changed");
Assertions.assertEquals("WAANZIN", Lemma.asWord(slotInfo[0].assign().w));
Assertions.assertEquals("WAANZIN", Lemma.asWord(slotInfo[0].assign().w, Export.BYTES.get()));
Assertions.assertEquals(-1L, grid.lo);
Assertions.assertEquals(-1L, grid.hi);
var g = new Gridded(grid);
var g = new Gridded(grid, mask.c());
g.gridToString(mask.c());
var aa = new PuzzleResult(mask, g, slotInfo, filled).exportFormatFromFilled(1, new Rewards(1, 1, 1));
System.out.println(String.join("\n", aa.grid()));

View File

@@ -1,24 +1,22 @@
package puzzle;
import lombok.val;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import puzzle.Export.Clued;
import puzzle.Export.Gridded;
import puzzle.SwedishGenerator.DictEntry;
import puzzle.SwedishGenerator.Rng;
import puzzle.SwedishGenerator.Slotinfo;
import puzzle.SwedishGenerator.Grid;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static puzzle.SwedishGenerator.fillMask;
public class PerformanceTest {
final DictEntry[] EN = DictData.DICT.index();
final DictEntry[] EN = DictData.DICT.index();
@Test
void testPerformance() {
val rng = new Rng(42);
@@ -45,7 +43,6 @@ public class PerformanceTest {
val mask = masker.generateMask(size, 100, 50, 20);
val slotInfo = Masker.slots(mask, EN);
val grid = mask.toGrid();
long t0 = System.currentTimeMillis();
// Try to fill multiple times to get a better average
@@ -54,7 +51,7 @@ public class PerformanceTest {
long totalBacktracks = 0;
int successCount = 0;
for (int i = 0; i < iterations; i++) {
val result = fillMask(rng, slotInfo, grid.copy(), false);
val result = fillMask(rng, slotInfo, Slotinfo.grid(slotInfo));
if (result.ok()) successCount++;
totalNodes += result.nodes();
totalBacktracks += result.backtracks();
@@ -66,7 +63,7 @@ public class PerformanceTest {
size, successCount, iterations, totalNodes / iterations, totalBacktracks / iterations, totalDuration);
}
}
void main() {
void main() {
testIncrementalComplexity();
}
@Test
@@ -81,7 +78,7 @@ public class PerformanceTest {
" 2 1 \n" +
" 1 \n" +
"221 22\n";
val mask = Masker.Clues.parse(maskStr);
val mask = Clued.parse(maskStr);
val allSlots = Masker.slots(mask.c(), EN);
//mask.toGrid()
System.out.println("[DEBUG_LOG] \n--- Incremental Complexity Test ---");
@@ -90,11 +87,11 @@ public class PerformanceTest {
for (int i = 10; i <= allSlots.length; i++) {
val subset = Arrays.copyOf(allSlots, i);
// Arrays.sort(subset, Comparator.comparingInt(Slotinfo::score));
// Arrays.sort(subset, Comparator.comparingInt(Slotinfo::score));
System.out.printf("[DEBUG_LOG] Testing with first %d slots%n of %s", i, allSlots.length);
var grid = Slotinfo.grid(subset);
visualizeSlots(subset);
measureFill(new Rng(123 + i), subset, grid, "Subset size " + i);
measureFill(new Rng(123 + i), subset, "Subset size " + i);
}
}
@@ -103,18 +100,18 @@ public class PerformanceTest {
val rng = new Rng(42);
// A single horizontal slot at (0,0)
val mask = Masker.Clues.parse("1 \n");
val mask = Clued.parse("1 \n");
val slots = Masker.slots(mask.c(), EN);
System.out.println("[DEBUG_LOG] \n--- Single Slot Resolution ---");
if (slots.length > 0) {
measureFill(rng, slots, mask.toGrid(), "Single Slot");
measureFill(rng, slots, "Single Slot");
} else {
System.out.println("[DEBUG_LOG] Error: No slots found in mask.");
}
}
private void measureFill(Rng rng, Slotinfo[] slots, Grid grid, String label) {
private void measureFill(Rng rng, Slotinfo[] slots, String label) {
long t0 = System.currentTimeMillis();
int iterations = 1;
long totalNodes = 0;
@@ -125,7 +122,7 @@ public class PerformanceTest {
// Reset assignments for each iteration
for (Slotinfo s : slots) s.assign().w = 0;
val result = fillMask(rng, slots, grid.copy(), false);
val result = fillMask(rng, slots, Slotinfo.grid(slots));
if (result.ok()) successCount++;
totalNodes += result.nodes();
totalBacktracks += result.backtracks();
@@ -148,8 +145,8 @@ public class PerformanceTest {
int dir = Masker.Slot.dir(key);
int clueIdx = Masker.Slot.clueIndex(key);
int cr = SwedishGenerator.IT[clueIdx].r();
int cc = SwedishGenerator.IT[clueIdx].c();
int cr = Masker.IT[clueIdx].r();
int cc = Masker.IT[clueIdx].c();
// User requested: aAAAA for a four letter to RIGHT clue slot.
// SwedishGenerator: 1=RIGHT, 0=DOWN, 2=UP, 3=LEFT
@@ -179,9 +176,9 @@ public class PerformanceTest {
display[cr][cc] = clueChar;
Masker.Slot.from(slot.key(), slot.lo(), slot.hi(), null).walk().forEach(idx -> {
int r = SwedishGenerator.IT[idx].r();
int c = SwedishGenerator.IT[idx].c();
Gridded.cellWalk((byte) slot.key(), slot.lo(), slot.hi()).forEach(idx -> {
int r = Masker.IT[idx].r();
int c = Masker.IT[idx].c();
if (display[r][c] == ' ' || (display[r][c] >= 'A' && display[r][c] <= 'D')) {
if (display[r][c] != ' ' && display[r][c] != slotChar) {
display[r][c] = '+'; // Intersection

View File

@@ -8,6 +8,7 @@ import puzzle.Export.Dicts;
import puzzle.Export.Gridded;
import puzzle.Export.IntListDTO;
import puzzle.Export.LetterVisit.LetterAt;
import puzzle.Masker.Clues;
import puzzle.Masker.Slot;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;
@@ -27,74 +28,81 @@ public class SwedishGeneratorTest {
public static Context get() { return CTX.get(); }
}
static final long TEST = Lemma.from("TEST");
static final long TEST = Lemma.from("TEST");
static final long IN = Lemma.from("IN");
static final long INER = Lemma.from("INER");
static final long INEREN = Lemma.from("INEREN");
static final long INERENA = Lemma.from("INERENA");
static final long INERENAE = Lemma.from("INERENAE");
static final long APPLE = Lemma.from("APPLE");
static final long EXE = Lemma.from("AXE");
static final long ABC = Lemma.from("ABC");
static final long ABD = Lemma.from("ABD");
static final long AZ = Lemma.from("AZ");
static final long AB = Lemma.from("AB");
static final long[] WORDS = new long[]{
Lemma.from("AT"),
Lemma.from("CAT"),
Lemma.from("DOGS"),
Lemma.from("APPLE"),
APPLE,
Lemma.from("APPLY"),
Lemma.from("BANAN"),
Lemma.from("BANANA"),
Lemma.from("BANANAS"),
Lemma.from("BANANASS") // length 8
};
static final long l2a = Lemma.from("IN");
static final long l4a = Lemma.from("INER");
static final long l6a = Lemma.from("INEREN");
static final long l7a = Lemma.from("INERENA");
static final long l8a = Lemma.from("INERENAE");
static final long l1 = Lemma.from("APPLE");
static final long l2 = Lemma.from("AXE");
static final long[] WORDS2 = new long[]{ Lemma.from("IN"),
Lemma.from("APPLE"),
Lemma.from("APPLY"),
Lemma.from("BANAN"),
Lemma.from("INE"),
Lemma.from("INER"),
Lemma.from("INEREN"),
Lemma.from("INERENA"),
Lemma.from("INERENAE") };
static final long ABC = Lemma.from("ABC");
static final long ABD = Lemma.from("ABD");
static final long AZ = Lemma.from("AZ");
static final long AB = Lemma.from("AB");
static final byte LETTER_A = ((byte) 'A') & 31;
static final byte LETTER_B = ((byte) 'B') & 31;
static final byte LETTER_C = ((byte) 'C') & 31;
static final byte LETTER_X = ((byte) 'X') & 31;
static final byte LETTER_Z = ((byte) 'Z') & 31;
static final byte CLUE_DOWN = 0;
static final byte CLUE_RIGHT = 1;
static final byte CLUE_UP = 2;
static final byte CLUE_LEFT = 3;
static final long[] WORDS2 = new long[]{ IN,
APPLE,
Lemma.from("APPLY"),
Lemma.from("BANAN"),
Lemma.from("INE"),
INER,
INEREN,
INERENA,
INERENAE };
static final int OFF_1_0 = Grid.offset(1, 0);
static final int OFF_1_1 = Grid.offset(1, 1);
static final int OFF_1_2 = Grid.offset(1, 2);
static final int OFF_1_3 = Grid.offset(1, 3);
static final int OFF_1_4 = Grid.offset(1, 4);
static final int OFF_1_5 = Grid.offset(1, 5);
static final int OFF_2_1 = Grid.offset(2, 1);
static final int OFF_2_3 = Grid.offset(2, 3);
static final int OFF_2_2 = Grid.offset(2, 2);
static final int OFF_2_4 = Grid.offset(2, 4);
static final int OFF_0_0 = Grid.offset(0, 0);
static final int OFF_0_4 = Grid.offset(0, 4);
static final int OFF_0_5 = Grid.offset(0, 5);
static final int OFF_0_1 = Grid.offset(0, 1);
static final int OFF_0_2 = Grid.offset(0, 2);
static final int OFF_0_3 = Grid.offset(0, 3);
static final int OFF_2_0 = Grid.offset(2, 0);
static final int OFF_2_5 = Grid.offset(2, 5);
static final int OFF_3_5 = Grid.offset(3, 5);
static final int OFF_4_5 = Grid.offset(4, 5);
static final int OFF_3_0 = Grid.offset(3, 0);
static final int OFF_3_1 = Grid.offset(3, 1);
static final int OFF_3_2 = Grid.offset(3, 2);
static final int OFF_3_3 = Grid.offset(3, 3);
static final int OFF_3_4 = Grid.offset(3, 4);
static final byte LETTER_A = ((byte) 'A') & 31;
static final byte LETTER_P = ((byte) 'P') & 31;
static final byte LETTER_L = ((byte) 'L') & 31;
static final byte LETTER_B = ((byte) 'B') & 31;
static final byte LETTER_C = ((byte) 'C') & 31;
static final byte LETTER_E = ((byte) 'E') & 31;
static final byte LETTER_I = ((byte) 'I') & 31;
static final byte LETTER_N = ((byte) 'N') & 31;
static final byte LETTER_X = ((byte) 'X') & 31;
static final byte LETTER_R = ((byte) 'R') & 31;
static final byte LETTER_Z = ((byte) 'Z') & 31;
static final byte CLUE_DOWN = 0;
static final byte CLUE_RIGHT = 1;
static final byte CLUE_UP = 2;
static final byte CLUE_LEFT = 3;
static final int OFF_1_0 = Masker.offset(1, 0);
static final int OFF_1_1 = Masker.offset(1, 1);
static final int OFF_1_2 = Masker.offset(1, 2);
static final int OFF_1_3 = Masker.offset(1, 3);
static final int OFF_1_4 = Masker.offset(1, 4);
static final int OFF_1_5 = Masker.offset(1, 5);
static final int OFF_2_1 = Masker.offset(2, 1);
static final int OFF_2_3 = Masker.offset(2, 3);
static final int OFF_2_2 = Masker.offset(2, 2);
static final int OFF_2_4 = Masker.offset(2, 4);
static final int OFF_0_0 = Masker.offset(0, 0);
static final int OFF_0_4 = Masker.offset(0, 4);
static final int OFF_0_5 = Masker.offset(0, 5);
static final int OFF_0_1 = Masker.offset(0, 1);
static final int OFF_0_2 = Masker.offset(0, 2);
static final int OFF_0_3 = Masker.offset(0, 3);
static final int OFF_2_0 = Masker.offset(2, 0);
static final int OFF_2_5 = Masker.offset(2, 5);
static final int OFF_3_5 = Masker.offset(3, 5);
static final int OFF_4_5 = Masker.offset(4, 5);
static final int OFF_3_0 = Masker.offset(3, 0);
static final int OFF_3_1 = Masker.offset(3, 1);
static final int OFF_3_2 = Masker.offset(3, 2);
static final int OFF_3_3 = Masker.offset(3, 3);
static final int OFF_3_4 = Masker.offset(3, 4);
static final byte D_BYTE_2 = CLUE_RIGHT;
enum Idx {
@@ -139,9 +147,9 @@ public class SwedishGeneratorTest {
}
@Test
void testPatternForSlotAllLetters() {
var grid = new Gridded(createEmpty());
var key = Slot.packSlotKey(OFF_0_0, CLUE_RIGHT);
val clues = Masker.Clues.createEmpty();
var grid = new Gridded(clues);
clues.setClueLo(IDX_0_0.lo, CLUE_RIGHT);
GridBuilder.placeWord(grid.grid(), grid.grid().g, key, (1L << OFF_0_1) | (1L << OFF_0_2) | (1L << OFF_0_3), 0L, ABC);
val map = grid.stream(clues).collect(Collectors.toMap(LetterAt::index, LetterAt::letter));
@@ -192,13 +200,21 @@ public class SwedishGeneratorTest {
var f = rng.nextFloat();
assertTrue(f >= 0.0 && f <= 1.0);
}
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
assertTrue(rng.biasedIndexPow3(100) >= 0 && rng.biasedIndexPow3(100) < 100);
}
@Test
void testGrid() {
var grid = new Gridded(createEmpty());
var empty = Clues.createEmpty();
var grid = new Gridded(empty);
GridBuilder.placeWord(grid.grid(), grid.grid().g, Slot.packSlotKey(0, CLUE_RIGHT), 1L << OFF_0_0, 0, Lemma.from("A"));
val arr = grid.stream(Masker.Clues.createEmpty()).collect(Collectors.toMap(LetterAt::index, LetterAt::letter));
val arr = grid.stream(empty).collect(Collectors.toMap(LetterAt::index, LetterAt::letter));
assertEquals(1, arr.size());
assertEquals(LETTER_A, arr.get(OFF_0_0));
}
@@ -217,11 +233,18 @@ public class SwedishGeneratorTest {
@Test
void testLemmaAndDict() {
Assertions.assertEquals(Lemma.packShiftIn("APPLE".getBytes(StandardCharsets.US_ASCII)), Lemma.unpackLetters(l1));
assertEquals(4, Lemma.unpackSize(l1));
assertEquals(LETTER_A, Lemma.byteAt(l1, 0));
Assertions.assertEquals(Lemma.packShiftIn("APPLE".getBytes(StandardCharsets.US_ASCII)), Lemma.unpackLetters(APPLE));
assertEquals(4, Lemma.unpackSize(APPLE));
assertEquals(LETTER_I, Lemma.byteAt(INERENAE, 0));
assertEquals(LETTER_N, Lemma.byteAt(INERENAE, 1));
assertEquals(LETTER_E, Lemma.byteAt(INERENAE, 2));
assertEquals(LETTER_R, Lemma.byteAt(INERENAE, 3));
assertEquals(LETTER_E, Lemma.byteAt(INERENAE, 4));
assertEquals(LETTER_N, Lemma.byteAt(INERENAE, 5));
assertEquals(LETTER_A, Lemma.byteAt(INERENAE, 6));
assertEquals(LETTER_E, Lemma.byteAt(INERENAE, 7));
var dict = Dicts.makeDict(new long[]{ l1, l2, l2a, l4a, l6a, l7a, l8a });
var dict = Dicts.makeDict(new long[]{ APPLE, EXE, IN, INER, INEREN, INERENA, INERENAE });
assertEquals(1, dict.index()[3].words().length);
assertEquals(1, dict.index()[5].words().length);
@@ -251,13 +274,13 @@ public class SwedishGeneratorTest {
assertEquals(OFF_2_3, Slot.clueIndex(key));
assertEquals(CLUE_DOWN, Slot.dir(key));
assertFalse(Slot.horiz(key));
var cells = Gridded.walk((byte) key, lo, 0L).toArray();
assertEquals(2, SwedishGenerator.IT[cells[0]].r());
assertEquals(3, SwedishGenerator.IT[cells[1]].r());
assertEquals(4, SwedishGenerator.IT[cells[2]].r());
assertEquals(5, SwedishGenerator.IT[cells[0]].c());
assertEquals(5, SwedishGenerator.IT[cells[1]].c());
assertEquals(5, SwedishGenerator.IT[cells[2]].c());
var cells = Gridded.cellWalk((byte) key, lo, 0L).toArray();
assertEquals(2, Masker.IT[cells[0]].r());
assertEquals(3, Masker.IT[cells[1]].r());
assertEquals(4, Masker.IT[cells[2]].r());
assertEquals(5, Masker.IT[cells[0]].c());
assertEquals(5, Masker.IT[cells[1]].c());
assertEquals(5, Masker.IT[cells[2]].c());
assertTrue(Slot.horiz(CLUE_RIGHT)); // right
assertFalse(Slot.horiz(CLUE_DOWN)); // down
@@ -335,7 +358,8 @@ public class SwedishGeneratorTest {
@Test
void testPlaceWord() {
var grid = new Gridded(createEmpty());
var empty = Clues.createEmpty();
var grid = new Gridded(empty);
// Slot at OFF_0_0 length 3, horizontal (right)
var key = Slot.packSlotKey(0, CLUE_RIGHT);
var lo = (1L << OFF_0_0) | (1L << OFF_0_1) | (1L << OFF_0_2);
@@ -344,7 +368,8 @@ public class SwedishGeneratorTest {
// 1. Successful placement in empty grid
assertTrue(GridBuilder.placeWord(grid.grid(), grid.grid().g, key, lo, hi, w1));
var map = grid.stream(Masker.Clues.createEmpty()).collect(Collectors.toMap(LetterAt::index, LetterAt::letter));
var map = grid.stream(empty).collect(Collectors.toMap(LetterAt::index, LetterAt::letter));
assertEquals(3, map.size());
assertEquals(LETTER_A, map.get(OFF_0_0));
assertEquals(LETTER_B, map.get(OFF_0_1));
@@ -362,7 +387,7 @@ public class SwedishGeneratorTest {
assertEquals(LETTER_C, map.get(OFF_0_2));
// 4. Partial placement then conflict (rollback)
grid = new Gridded(createEmpty());
grid = new Gridded(Clues.createEmpty());
GridBuilder.placeWord(grid.grid(), grid.grid().g, Slot.packSlotKey(0, CLUE_RIGHT), 1L << OFF_0_2, 0, Lemma.from("X")); // Conflict at the end
assertFalse(GridBuilder.placeWord(grid.grid(), grid.grid().g, key, lo, hi, w1));
map = grid.stream(Masker.Clues.createEmpty()).collect(Collectors.toMap(LetterAt::index, LetterAt::letter));
@@ -372,7 +397,8 @@ public class SwedishGeneratorTest {
@Test
void testBacktrackingHelpers() {
var grid = new Gridded(createEmpty());
var clues = Clues.createEmpty();
var grid = new Gridded(clues);
// Slot at 0,1 length 2
var key = Slot.packSlotKey(0, CLUE_RIGHT);
var lo = (1L << OFF_0_1) | (1L << OFF_0_2);
@@ -381,7 +407,8 @@ public class SwedishGeneratorTest {
val top = grid.grid().hi;
var placed = GridBuilder.placeWord(grid.grid(), grid.grid().g, key, lo, 0L, w);
assertTrue(placed);
var map = grid.stream(Masker.Clues.createEmpty()).collect(Collectors.toMap(LetterAt::index, LetterAt::letter));
var map = grid.stream(clues).collect(Collectors.toMap(LetterAt::index, LetterAt::letter));
assertEquals(2, map.size());
assertEquals(LETTER_A, map.get(OFF_0_1));
assertEquals(LETTER_Z, map.get(OFF_0_2));