introduce bitloops

This commit is contained in:
mike
2026-01-17 03:14:13 +01:00
parent 57be64c37e
commit 44f53801a3
5 changed files with 57 additions and 51 deletions

View File

@@ -7,9 +7,9 @@ import lombok.val;
import puzzle.Export.Gridded.Replacar.Cell; import puzzle.Export.Gridded.Replacar.Cell;
import puzzle.Export.LetterVisit.LetterAt; import puzzle.Export.LetterVisit.LetterAt;
import puzzle.SwedishGenerator.Clues; import puzzle.SwedishGenerator.Clues;
import puzzle.SwedishGenerator.DictEntry;
import puzzle.SwedishGenerator.FillResult; import puzzle.SwedishGenerator.FillResult;
import puzzle.SwedishGenerator.Grid; import puzzle.SwedishGenerator.Grid;
import puzzle.SwedishGenerator.Slotinfo;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
@@ -225,22 +225,23 @@ public record Export() {
public record ExportedPuzzle(String[] grid, WordOut[] words, int difficulty, Rewards rewards) { } public record ExportedPuzzle(String[] grid, WordOut[] words, int difficulty, Rewards rewards) { }
public record PuzzleResult(Clued clues, FillResult filled) { public record PuzzleResult(Clued clues, Slotinfo[] slots, FillResult filled) {
Placed extractPlacedFromSlot(Slot s, long lemma) { return new Placed(lemma, s.key(), s.walk().toArray()); }
public ExportedPuzzle exportFormatFromFilled(int difficulty, Rewards rewards) { public ExportedPuzzle exportFormatFromFilled(int difficulty, Rewards rewards) {
var g = filled().grid(); var g = filled().grid();
var placed = new ArrayList<Placed>(); var placed = new ArrayList<Placed>();
var clueMap = filled().clueMap(); for (var slot : slots) {
val entries = new DictEntry[10]; placed.add(new Placed(slot.assign().w, slot.key(), Gridded.walk((byte) slot.key(), slot.lo(), slot.hi()).toArray()));
clues.forEachSlot((int key, long lo, long hi) -> { }
/* clues.forEachSlot((int key, long lo, long hi) -> {
var word = clueMap[key]; var word = clueMap[key];
if (word != 0L) { if (word != 0L) {
placed.add(extractPlacedFromSlot(Slot.from(key, lo, hi, entries[Slot.length(lo, hi)]), word)); placed.add(extractPlacedFromSlot(Slot.from(key, lo, hi, entries[Slot.length(lo, hi)]), word));
} else { } else {
System.err.println("Could not find clue for slot: " + key); System.err.println("Could not find clue for slot: " + key);
} }
}); });*/
// If nothing placed: return full grid mapped to letters/# only // If nothing placed: return full grid mapped to letters/# only
if (placed.isEmpty()) { if (placed.isEmpty()) {

View File

@@ -97,7 +97,7 @@ public class Main {
} }
section("Result"); section("Result");
res.filled().calcSimpel(); res.filled().stats().simplicity = FillResult.calcSimpel(res.slots());
info(String.format(Locale.ROOT, "simplicity : %.2f", res.filled().stats().simplicity)); info(String.format(Locale.ROOT, "simplicity : %.2f", res.filled().stats().simplicity));
section("Mask"); section("Mask");
@@ -396,9 +396,9 @@ public class Main {
TOTAL_NODES.addAndGet(filled.stats().nodes); TOTAL_NODES.addAndGet(filled.stats().nodes);
TOTAL_BACKTRACKS.addAndGet(filled.stats().backtracks); TOTAL_BACKTRACKS.addAndGet(filled.stats().backtracks);
if (filled.ok()) { if (filled.ok()) {
filled.calcSimpel(); val simpel = FillResult.calcSimpel(slotInfo);
TOTAL_SUCCESS.incrementAndGet(); TOTAL_SUCCESS.incrementAndGet();
TOTAL_SIMPLICITY.addAndGet((long) (filled.stats().simplicity * 100)); TOTAL_SIMPLICITY.addAndGet((long) (simpel * 100));
} }
var name = Thread.currentThread().getName(); var name = Thread.currentThread().getName();
@@ -413,7 +413,7 @@ public class Main {
); );
if (filled.ok() && (opts.minSimplicity <= 0 || filled.stats().simplicity >= opts.minSimplicity)) { if (filled.ok() && (opts.minSimplicity <= 0 || filled.stats().simplicity >= opts.minSimplicity)) {
return new PuzzleResult(new Clued(mask), filled); return new PuzzleResult(new Clued(mask), slotInfo, filled);
} }
if (opts.verbose && filled.ok()) { if (opts.verbose && filled.ok()) {

View File

@@ -2,6 +2,7 @@ package puzzle;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
import lombok.experimental.Delegate; import lombok.experimental.Delegate;
@@ -129,24 +130,19 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
public static record FillResult(boolean ok, public static record FillResult(boolean ok,
Gridded grid, Gridded grid,
long[] clueMap,
@Delegate FillStats stats) { @Delegate FillStats stats) {
public void calcSimpel() { static public long calcSimpel(Slotinfo[] slots) {
if (ok) {
int k = 0; int k = 0;
for (var n = 1; n < clueMap.length; n++) { long simpel = 0L;
if (clueMap[n] != X) { for (var n = 1; n < slots.length; n++) {
if (slots[n].assign().w != X) {
k++; k++;
stats.simplicity += Lemma.simpel(clueMap[n]); simpel += Lemma.simpel(slots[n].assign().w);
} }
} }
stats.simplicity = k == 0 ? 0 : stats.simplicity / k; simpel = k == 0 ? 0 : simpel / k;
} return simpel;
}
public int wordCount(int k) {
for (var n = 1; n < clueMap.length; n++) if (clueMap[n] != X) k++;
return k;
} }
} }
@@ -335,12 +331,20 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
} }
} }
@AllArgsConstructor
@NoArgsConstructor
static class Assign { static class Assign {
long w; long w;
} }
static record Slotinfo(int key, long lo, long hi, int score, Assign assign, DictEntry entry) { } static record Slotinfo(int key, long lo, long hi, int score, Assign assign, DictEntry entry) {
public static int wordCount(int k, Slotinfo[] arr) {
for (var n = 1; n < arr.length; n++) if (arr[n].assign.w != X) k++;
return k;
}
}
static record Slot(int key, long lo, long hi, DictEntry entry) { static record Slot(int key, long lo, long hi, DictEntry entry) {
@@ -849,6 +853,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
return true; return true;
} }
/// pattern cannot be X
static int[] candidateInfoForPattern(long[] res, long pattern, long[][] posBitsets, int numLongs) { static int[] candidateInfoForPattern(long[] res, long pattern, long[][] posBitsets, int numLongs) {
System.arraycopy(posBitsets[(int) (pattern & 0xFF) - 1], 0, res, 0, numLongs); System.arraycopy(posBitsets[(int) (pattern & 0xFF) - 1], 0, res, 0, numLongs);
for (long p = pattern >>> 8; p != X; p >>>= 8) { for (long p = pattern >>> 8; p != X; p >>>= 8) {
@@ -870,7 +875,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
return indices; return indices;
} }
/// pattern cannot be X
static int candidateCountForPattern(final long[] res, final long pattern, final long[][] posBitsets, final int numLongs) { static int candidateCountForPattern(final long[] res, final long pattern, final long[][] posBitsets, final int numLongs) {
System.arraycopy(posBitsets[(int) (pattern & 0xFF) - 1], 0, res, 0, numLongs); System.arraycopy(posBitsets[(int) (pattern & 0xFF) - 1], 0, res, 0, numLongs);
for (long p = pattern >>> 8; p != X; p >>>= 8) { for (long p = pattern >>> 8; p != X; p >>>= 8) {
@@ -1093,9 +1098,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
// final progress line // final progress line
grid.lo = solver.glo; grid.lo = solver.glo;
grid.hi = solver.ghi; grid.hi = solver.ghi;
val assigned = new long[CLUE_INDEX_MAX_SIZE]; var res = new FillResult(ok, new Gridded(grid),
Arrays.stream(slots).forEach(s -> assigned[s.key] = s.assign.w);
var res = new FillResult(ok, new Gridded(grid), assigned,
new FillStats(solver.nodes, solver.backtracks, (System.currentTimeMillis() - t0) / 1000.0, solver.lastMRV)); new FillStats(solver.nodes, solver.backtracks, (System.currentTimeMillis() - t0) / 1000.0, solver.lastMRV));
if (!multiThreaded) { if (!multiThreaded) {
System.out.print("\r" + Strings.padRight("", 120) + "\r"); System.out.print("\r" + Strings.padRight("", 120) + "\r");
@@ -1106,7 +1109,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
System.out.println( System.out.println(
String.format(Locale.ROOT, String.format(Locale.ROOT,
"[######################] %d/%d slots | nodes=%d | backtracks=%d | mrv=%d | %.1fs", "[######################] %d/%d slots | nodes=%d | backtracks=%d | mrv=%d | %.1fs",
res.wordCount(0), TOTAL, res.nodes(), res.backtracks(), res.lastMRV(), res.seconds() Slotinfo.wordCount(0, slots), TOTAL, res.nodes(), res.backtracks(), res.lastMRV(), res.seconds()
) )
); );
} }

View File

@@ -8,8 +8,10 @@ import puzzle.Export.Gridded;
import puzzle.Export.Placed; import puzzle.Export.Placed;
import puzzle.Export.PuzzleResult; import puzzle.Export.PuzzleResult;
import puzzle.Export.Rewards; import puzzle.Export.Rewards;
import puzzle.SwedishGenerator.Assign;
import puzzle.SwedishGenerator.FillResult; import puzzle.SwedishGenerator.FillResult;
import puzzle.SwedishGenerator.Rng; import puzzle.SwedishGenerator.Rng;
import puzzle.SwedishGenerator.Slotinfo;
import puzzle.SwedishGeneratorTest.Idx; import puzzle.SwedishGeneratorTest.Idx;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Paths; import java.nio.file.Paths;
@@ -64,15 +66,16 @@ public class ExportFormatTest {
clues.setClueLo(Idx.IDX_0_5.lo, CLUE_LEFT); clues.setClueLo(Idx.IDX_0_5.lo, CLUE_LEFT);
var grid = new Gridded(clues.toGrid()); var grid = new Gridded(clues.toGrid());
var clueMap = new long[SwedishGenerator.CLUE_INDEX_MAX_SIZE];
// key = (cellIndex << 2) | (direction) // key = (cellIndex << 2) | (direction)
var key = Slot.packSlotKey(0, CLUE_RIGHT); var key = Slot.packSlotKey(0, CLUE_RIGHT);
var lo = (1L << OFF_0_1) | (1L << OFF_0_2) | (1L << OFF_0_3) | (1L << OFF_0_4); var lo = (1L << OFF_0_1) | (1L << OFF_0_2) | (1L << OFF_0_3) | (1L << OFF_0_4);
clueMap[key] = TEST;
assertTrue(placeWord(grid.grid(), grid.grid().g, key, lo, 0L, TEST)); assertTrue(placeWord(grid.grid(), grid.grid().g, key, lo, 0L, TEST));
var fillResult = new FillResult(true, grid, clueMap, new FillStats(0, 0, 0, 0)); var fillResult = new FillResult(true, grid, new FillStats(0, 0, 0, 0));
var puzzleResult = new PuzzleResult(new Clued(clues), fillResult); var puzzleResult = new PuzzleResult(new Clued(clues), new Slotinfo[]{
new Slotinfo(key, lo, 0L, 0, new Assign(TEST), null)
}, fillResult);
var rewards = new Rewards(10, 5, 1); var rewards = new Rewards(10, 5, 1);
var exported = puzzleResult.exportFormatFromFilled(2, rewards); var exported = puzzleResult.exportFormatFromFilled(2, rewards);
@@ -111,8 +114,8 @@ public class ExportFormatTest {
void testExportFormatEmpty() { void testExportFormatEmpty() {
var grid = SwedishGeneratorTest.createEmpty(); var grid = SwedishGeneratorTest.createEmpty();
val clues = Clues.createEmpty(); val clues = Clues.createEmpty();
var fillResult = new FillResult(true, new Gridded(grid), new long[300], new FillStats(0, 0, 0, 0)); var fillResult = new FillResult(true, new Gridded(grid), new FillStats(0, 0, 0, 0));
var puzzleResult = new PuzzleResult(new Clued(clues), fillResult); var puzzleResult = new PuzzleResult(new Clued(clues), new Slotinfo[0], fillResult);
var exported = puzzleResult.exportFormatFromFilled(1, new Rewards(0, 0, 0)); var exported = puzzleResult.exportFormatFromFilled(1, new Rewards(0, 0, 0));

View File

@@ -2,7 +2,6 @@ package puzzle;
import lombok.val; import lombok.val;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import puzzle.Export.ClueAt; import puzzle.Export.ClueAt;
import puzzle.Export.Clued; import puzzle.Export.Clued;
@@ -185,12 +184,12 @@ public class MainTest {
val slotInfo = scoreSlots(new int[slots.length], slots); val slotInfo = scoreSlots(new int[slots.length], slots);
var filled = fillMask(rng, slotInfo, mask.toGrid(), false); var filled = fillMask(rng, slotInfo, mask.toGrid(), false);
Assertions.assertTrue(filled.ok(), "Puzzle generation failed (not ok)"); Assertions.assertTrue(filled.ok(), "Puzzle generation failed (not ok)");
Assertions.assertEquals(18, filled.wordCount(0), "Number of assigned words changed"); Assertions.assertEquals(17, Slotinfo.wordCount(0, slotInfo), "Number of assigned words changed");
Assertions.assertEquals("SLEDE", Lemma.asWord(filled.clueMap()[282])); Assertions.assertEquals("POENIGE", Lemma.asWord(slotInfo[0].assign().w));
Assertions.assertEquals(-1L, filled.grid().grid().lo); Assertions.assertEquals(-1L, filled.grid().grid().lo);
Assertions.assertEquals(255L, filled.grid().grid().hi); Assertions.assertEquals(255L, filled.grid().grid().hi);
filled.grid().gridToString(mask); filled.grid().gridToString(mask);
var aa = new PuzzleResult(new Clued(mask), filled).exportFormatFromFilled(1, new Rewards(1, 1, 1)); var aa = new PuzzleResult(new Clued(mask), slotInfo, filled).exportFormatFromFilled(1, new Rewards(1, 1, 1));
} }
@Test @Test
@@ -204,7 +203,7 @@ public class MainTest {
foundSeed = seed; foundSeed = seed;
System.out.println("[DEBUG_LOG] Seed found: " + seed); System.out.println("[DEBUG_LOG] Seed found: " + seed);
System.out.println("[DEBUG_LOG] Simplicity: " + res.filled().stats().simplicity); System.out.println("[DEBUG_LOG] Simplicity: " + res.filled().stats().simplicity);
System.out.println("[DEBUG_LOG] ClueMap Size: " + res.filled().wordCount(0)); System.out.println("[DEBUG_LOG] ClueMap Size: " + Slotinfo.wordCount(0, res.slots()));
System.out.println("[DEBUG_LOG] Grid:"); System.out.println("[DEBUG_LOG] Grid:");
System.out.println(res.filled().grid().renderHuman(res.clues().c())); System.out.println(res.filled().grid().renderHuman(res.clues().c()));
System.out.println(res.filled().grid().gridToString(res.clues().c())); System.out.println(res.filled().grid().gridToString(res.clues().c()));