introduce bitloops
This commit is contained in:
@@ -7,9 +7,9 @@ import lombok.val;
|
||||
import puzzle.Export.Gridded.Replacar.Cell;
|
||||
import puzzle.Export.LetterVisit.LetterAt;
|
||||
import puzzle.SwedishGenerator.Clues;
|
||||
import puzzle.SwedishGenerator.DictEntry;
|
||||
import puzzle.SwedishGenerator.FillResult;
|
||||
import puzzle.SwedishGenerator.Grid;
|
||||
import puzzle.SwedishGenerator.Slotinfo;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
@@ -225,22 +225,23 @@ public record Export() {
|
||||
|
||||
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) {
|
||||
var g = filled().grid();
|
||||
var placed = new ArrayList<Placed>();
|
||||
var clueMap = filled().clueMap();
|
||||
val entries = new DictEntry[10];
|
||||
clues.forEachSlot((int key, long lo, long hi) -> {
|
||||
for (var slot : slots) {
|
||||
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) -> {
|
||||
var word = clueMap[key];
|
||||
if (word != 0L) {
|
||||
placed.add(extractPlacedFromSlot(Slot.from(key, lo, hi, entries[Slot.length(lo, hi)]), word));
|
||||
} else {
|
||||
System.err.println("Could not find clue for slot: " + key);
|
||||
}
|
||||
});
|
||||
});*/
|
||||
|
||||
// If nothing placed: return full grid mapped to letters/# only
|
||||
if (placed.isEmpty()) {
|
||||
|
||||
@@ -97,7 +97,7 @@ public class Main {
|
||||
}
|
||||
|
||||
section("Result");
|
||||
res.filled().calcSimpel();
|
||||
res.filled().stats().simplicity = FillResult.calcSimpel(res.slots());
|
||||
info(String.format(Locale.ROOT, "simplicity : %.2f", res.filled().stats().simplicity));
|
||||
|
||||
section("Mask");
|
||||
@@ -396,9 +396,9 @@ public class Main {
|
||||
TOTAL_NODES.addAndGet(filled.stats().nodes);
|
||||
TOTAL_BACKTRACKS.addAndGet(filled.stats().backtracks);
|
||||
if (filled.ok()) {
|
||||
filled.calcSimpel();
|
||||
val simpel = FillResult.calcSimpel(slotInfo);
|
||||
TOTAL_SUCCESS.incrementAndGet();
|
||||
TOTAL_SIMPLICITY.addAndGet((long) (filled.stats().simplicity * 100));
|
||||
TOTAL_SIMPLICITY.addAndGet((long) (simpel * 100));
|
||||
}
|
||||
|
||||
var name = Thread.currentThread().getName();
|
||||
@@ -413,7 +413,7 @@ public class Main {
|
||||
);
|
||||
|
||||
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()) {
|
||||
|
||||
@@ -2,6 +2,7 @@ package puzzle;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
import lombok.experimental.Delegate;
|
||||
@@ -129,24 +130,19 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
|
||||
|
||||
public static record FillResult(boolean ok,
|
||||
Gridded grid,
|
||||
long[] clueMap,
|
||||
@Delegate FillStats stats) {
|
||||
|
||||
public void calcSimpel() {
|
||||
if (ok) {
|
||||
static public long calcSimpel(Slotinfo[] slots) {
|
||||
int k = 0;
|
||||
for (var n = 1; n < clueMap.length; n++) {
|
||||
if (clueMap[n] != X) {
|
||||
long simpel = 0L;
|
||||
for (var n = 1; n < slots.length; n++) {
|
||||
if (slots[n].assign().w != X) {
|
||||
k++;
|
||||
stats.simplicity += Lemma.simpel(clueMap[n]);
|
||||
simpel += Lemma.simpel(slots[n].assign().w);
|
||||
}
|
||||
}
|
||||
stats.simplicity = k == 0 ? 0 : stats.simplicity / k;
|
||||
}
|
||||
}
|
||||
public int wordCount(int k) {
|
||||
for (var n = 1; n < clueMap.length; n++) if (clueMap[n] != X) k++;
|
||||
return k;
|
||||
simpel = k == 0 ? 0 : simpel / k;
|
||||
return simpel;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -335,12 +331,20 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
|
||||
}
|
||||
}
|
||||
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
static class Assign {
|
||||
|
||||
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) {
|
||||
|
||||
@@ -849,6 +853,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/// pattern cannot be X
|
||||
static int[] candidateInfoForPattern(long[] res, long pattern, long[][] posBitsets, int numLongs) {
|
||||
System.arraycopy(posBitsets[(int) (pattern & 0xFF) - 1], 0, res, 0, numLongs);
|
||||
for (long p = pattern >>> 8; p != X; p >>>= 8) {
|
||||
@@ -870,7 +875,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
|
||||
|
||||
return indices;
|
||||
}
|
||||
|
||||
/// pattern cannot be X
|
||||
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);
|
||||
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
|
||||
grid.lo = solver.glo;
|
||||
grid.hi = solver.ghi;
|
||||
val assigned = new long[CLUE_INDEX_MAX_SIZE];
|
||||
Arrays.stream(slots).forEach(s -> assigned[s.key] = s.assign.w);
|
||||
var res = new FillResult(ok, new Gridded(grid), assigned,
|
||||
var res = new FillResult(ok, new Gridded(grid),
|
||||
new FillStats(solver.nodes, solver.backtracks, (System.currentTimeMillis() - t0) / 1000.0, solver.lastMRV));
|
||||
if (!multiThreaded) {
|
||||
System.out.print("\r" + Strings.padRight("", 120) + "\r");
|
||||
@@ -1106,7 +1109,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
|
||||
System.out.println(
|
||||
String.format(Locale.ROOT,
|
||||
"[######################] %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()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -8,8 +8,10 @@ import puzzle.Export.Gridded;
|
||||
import puzzle.Export.Placed;
|
||||
import puzzle.Export.PuzzleResult;
|
||||
import puzzle.Export.Rewards;
|
||||
import puzzle.SwedishGenerator.Assign;
|
||||
import puzzle.SwedishGenerator.FillResult;
|
||||
import puzzle.SwedishGenerator.Rng;
|
||||
import puzzle.SwedishGenerator.Slotinfo;
|
||||
import puzzle.SwedishGeneratorTest.Idx;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
@@ -64,15 +66,16 @@ public class ExportFormatTest {
|
||||
clues.setClueLo(Idx.IDX_0_5.lo, CLUE_LEFT);
|
||||
var grid = new Gridded(clues.toGrid());
|
||||
|
||||
var clueMap = new long[SwedishGenerator.CLUE_INDEX_MAX_SIZE];
|
||||
// key = (cellIndex << 2) | (direction)
|
||||
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);
|
||||
clueMap[key] = 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 puzzleResult = new PuzzleResult(new Clued(clues), fillResult);
|
||||
var fillResult = new FillResult(true, grid, new FillStats(0, 0, 0, 0));
|
||||
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 exported = puzzleResult.exportFormatFromFilled(2, rewards);
|
||||
@@ -111,8 +114,8 @@ public class ExportFormatTest {
|
||||
void testExportFormatEmpty() {
|
||||
var grid = SwedishGeneratorTest.createEmpty();
|
||||
val clues = Clues.createEmpty();
|
||||
var fillResult = new FillResult(true, new Gridded(grid), new long[300], new FillStats(0, 0, 0, 0));
|
||||
var puzzleResult = new PuzzleResult(new Clued(clues), fillResult);
|
||||
var fillResult = new FillResult(true, new Gridded(grid), new FillStats(0, 0, 0, 0));
|
||||
var puzzleResult = new PuzzleResult(new Clued(clues), new Slotinfo[0], fillResult);
|
||||
|
||||
var exported = puzzleResult.exportFormatFromFilled(1, new Rewards(0, 0, 0));
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package puzzle;
|
||||
|
||||
import lombok.val;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import puzzle.Export.ClueAt;
|
||||
import puzzle.Export.Clued;
|
||||
@@ -185,12 +184,12 @@ public class MainTest {
|
||||
val slotInfo = scoreSlots(new int[slots.length], slots);
|
||||
var filled = fillMask(rng, slotInfo, mask.toGrid(), false);
|
||||
Assertions.assertTrue(filled.ok(), "Puzzle generation failed (not ok)");
|
||||
Assertions.assertEquals(18, filled.wordCount(0), "Number of assigned words changed");
|
||||
Assertions.assertEquals("SLEDE", Lemma.asWord(filled.clueMap()[282]));
|
||||
Assertions.assertEquals(17, Slotinfo.wordCount(0, slotInfo), "Number of assigned words changed");
|
||||
Assertions.assertEquals("POENIGE", Lemma.asWord(slotInfo[0].assign().w));
|
||||
Assertions.assertEquals(-1L, filled.grid().grid().lo);
|
||||
Assertions.assertEquals(255L, filled.grid().grid().hi);
|
||||
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
|
||||
@@ -204,7 +203,7 @@ public class MainTest {
|
||||
foundSeed = seed;
|
||||
System.out.println("[DEBUG_LOG] Seed found: " + seed);
|
||||
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(res.filled().grid().renderHuman(res.clues().c()));
|
||||
System.out.println(res.filled().grid().gridToString(res.clues().c()));
|
||||
|
||||
Reference in New Issue
Block a user