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.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;
@@ -44,7 +44,7 @@ public record Export() {
static final String INIT = IntStream.range(0, R).mapToObj(l_ -> " ").collect(Collectors.joining("\n"));
public record ClueAt(int index, int clue) { }
public record ClueAt(int index, int clue) { }
public record Clued(@Delegate Clues c) {
@@ -62,14 +62,14 @@ public record Export() {
}
public Stream<ClueAt> stream() {
val stream = Stream.<ClueAt>builder();
for (var l = c.lo & ~c.rlo & c.vlo ; l != X; l &= l - 1) stream.accept(new ClueAt(Long.numberOfTrailingZeros(l), 1));
for (var l = c.lo & ~c.rlo & ~c.vlo ; l != X; l &= l - 1) stream.accept(new ClueAt(Long.numberOfTrailingZeros(l), 0));
for (var l = c.lo & c.rlo & ~c.vlo ; l != X; l &= l - 1) stream.accept(new ClueAt(Long.numberOfTrailingZeros(l), 2));
for (var l = c.lo & c.rlo & c.vlo ; l != X; l &= l - 1) stream.accept(new ClueAt(Long.numberOfTrailingZeros(l), 3));
for (var h = c.hi & ~c.rhi & c.vhi ; h != X; h &= h - 1) stream.accept(new ClueAt(64 | Long.numberOfTrailingZeros(h), 1));
for (var h = c.hi & ~c.rhi & ~c.vhi ; h != X; h &= h - 1) stream.accept(new ClueAt(64 | Long.numberOfTrailingZeros(h), 0));
for (var h = c.hi & c.rhi & ~c.vhi ; h != X; h &= h - 1) stream.accept(new ClueAt((64 | Long.numberOfTrailingZeros(h)), 2));
for (var h = c.hi & c.rhi & c.vhi ; h != X; h &= h - 1) stream.accept(new ClueAt((64 | Long.numberOfTrailingZeros(h)), 3));
for (var l = c.lo & ~c.rlo & c.vlo; l != X; l &= l - 1) stream.accept(new ClueAt(Long.numberOfTrailingZeros(l), 1));
for (var l = c.lo & ~c.rlo & ~c.vlo; l != X; l &= l - 1) stream.accept(new ClueAt(Long.numberOfTrailingZeros(l), 0));
for (var l = c.lo & c.rlo & ~c.vlo; l != X; l &= l - 1) stream.accept(new ClueAt(Long.numberOfTrailingZeros(l), 2));
for (var l = c.lo & c.rlo & c.vlo; l != X; l &= l - 1) stream.accept(new ClueAt(Long.numberOfTrailingZeros(l), 3));
for (var h = c.hi & ~c.rhi & c.vhi; h != X; h &= h - 1) stream.accept(new ClueAt(64 | Long.numberOfTrailingZeros(h), 1));
for (var h = c.hi & ~c.rhi & ~c.vhi; h != X; h &= h - 1) stream.accept(new ClueAt(64 | Long.numberOfTrailingZeros(h), 0));
for (var h = c.hi & c.rhi & ~c.vhi; h != X; h &= h - 1) stream.accept(new ClueAt((64 | Long.numberOfTrailingZeros(h)), 2));
for (var h = c.hi & c.rhi & c.vhi; h != X; h &= h - 1) stream.accept(new ClueAt((64 | Long.numberOfTrailingZeros(h)), 3));
return stream.build();
}
@@ -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()) {

View File

@@ -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()) {

View File

@@ -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) {
int k = 0;
for (var n = 1; n < clueMap.length; n++) {
if (clueMap[n] != X) {
k++;
stats.simplicity += Lemma.simpel(clueMap[n]);
}
static public long calcSimpel(Slotinfo[] slots) {
int k = 0;
long simpel = 0L;
for (var n = 1; n < slots.length; n++) {
if (slots[n].assign().w != X) {
k++;
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()
)
);
}