introduce bitloops

This commit is contained in:
mike
2026-01-14 12:28:07 +01:00
parent 6afe675a9d
commit dfb4679da8
5 changed files with 49 additions and 44 deletions

View File

@@ -6,6 +6,7 @@ import lombok.experimental.Delegate;
import lombok.val;
import puzzle.Export.Gridded.Replacar.Cell;
import puzzle.SwedishGenerator.Clues;
import puzzle.SwedishGenerator.DictEntry;
import puzzle.SwedishGenerator.FillResult;
import puzzle.SwedishGenerator.Grid;
import java.util.ArrayList;
@@ -197,10 +198,11 @@ public record Export() {
var g = filled().grid();
var placed = new ArrayList<Placed>();
var clueMap = filled().clueMap();
val entries = new DictEntry[10];
mask.forEachSlot((int key, long lo, long hi) -> {
var word = clueMap[key];
if (word != 0L) {
placed.add(extractPlacedFromSlot(Slot.from(key, lo, hi), word));
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);
}

View File

@@ -43,8 +43,8 @@ public class Main {
@NoArgsConstructor
public static class Opts {
public int seed = (int) (System.nanoTime() ^ System.currentTimeMillis());
public int pop = 18;
public int seed = (int) (System.nanoTime() ^ System.currentTimeMillis());
public int pop = 18;
public int gens = 1200;
public String wordsPath = "nl_score_hints_v3.csv";
public double minSimplicity = 0; // 0 means no limit
@@ -358,10 +358,10 @@ public class Main {
}
static PuzzleResult _attempt(Rng rng, Dict dict, Opts opts) {
TOTAL_ATTEMPTS.incrementAndGet();
var swe = new SwedishGenerator(rng, new int[STACK_SIZE], Clues.createEmpty());
var mask = swe.generateMask(opts.pop, opts.gens, Math.max(opts.pop, (int) Math.floor(opts.pop * 1.5)));
var swe = new SwedishGenerator(rng, new int[STACK_SIZE], Clues.createEmpty());
var mask = swe.generateMask(opts.pop, opts.gens, Math.max(opts.pop, (int) Math.floor(opts.pop * 1.5)));
var filled = fillMask(rng, extractSlots(mask), mask.toGrid(), dict.index());
var filled = fillMask(rng, extractSlots(mask, dict.index()), mask.toGrid());
TOTAL_NODES.addAndGet(filled.stats().nodes);
TOTAL_BACKTRACKS.addAndGet(filled.stats().backtracks);

View File

@@ -383,18 +383,17 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
}
}
static record Slot(int key, long lo, long hi) {
static record Slot(int key, long lo, long hi, DictEntry entry) {
static final int BIT_FOR_DIR = 2;
static Slot from(int key, long lo, long hi) { return new Slot(key, lo, hi); }
public int length() { return Long.bitCount(lo) + Long.bitCount(hi); }
public static int clueIndex(int key) { return key >>> BIT_FOR_DIR; }
public static int dir(int key) { return key & 3; }
public static boolean increasing(int dir) { return (dir & 2) == 0; }
public IntStream walk() { return Gridded.walk((byte) key, lo, hi); }
public static boolean horiz(int d) { return (d & 1) != 0; }
public static int packSlotDir(int idx, int d) { return (idx << BIT_FOR_DIR) | d; }
static Slot from(int key, long lo, long hi, DictEntry entry) { return new Slot(key, lo, hi, entry); }
public static int length(long lo, long hi) { return Long.bitCount(lo) + Long.bitCount(hi); }
public static int clueIndex(int key) { return key >>> BIT_FOR_DIR; }
public static int dir(int key) { return key & 3; }
public static boolean increasing(int dir) { return (dir & 2) == 0; }
public IntStream walk() { return Gridded.walk((byte) key, lo, hi); }
public static boolean horiz(int d) { return (d & 1) != 0; }
public static int packSlotDir(int idx, int d) { return (idx << BIT_FOR_DIR) | d; }
}
private static void processSlot(Clues grid, SlotVisitor visitor, int idx) {
@@ -438,10 +437,10 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
visitor.visit(key, rayLo, rayHi);
}
static Slot[] extractSlots(Clues grid) {
static Slot[] extractSlots(Clues grid, DictEntry[] index) {
var slots = new Slot[grid.clueCount()];
int[] N = new int[]{ 0 };
grid.forEachSlot((key, lo, hi) -> slots[N[0]++] = Slot.from(key, lo, hi));
grid.forEachSlot((key, lo, hi) -> slots[N[0]++] = Slot.from(key, lo, hi, index[Slot.length(lo, hi)]));
return slots;
}
@@ -764,11 +763,11 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
}
return p;
}
static int slotScore(byte[] count, Slot s) {
static int slotScore(byte[] count, long lo, long hi) {
int cross = 0;
for (long b = s.lo; b != 0; b &= b - 1) cross += (count[Long.numberOfTrailingZeros(b)] - 1);
for (long b = s.hi; b != 0; b &= b - 1) cross += (count[64 | Long.numberOfTrailingZeros(b)] - 1);
return cross * 10 + s.length();
for (long b = lo; b != 0; b &= b - 1) cross += (count[Long.numberOfTrailingZeros(b)] - 1);
for (long b = hi; b != 0; b &= b - 1) cross += (count[64 | Long.numberOfTrailingZeros(b)] - 1);
return cross * 10 + Slot.length(lo, hi);
}
static boolean placeWord(Grid grid, final int key, final long lo, final long hi, final long w) {
final long glo = grid.lo, ghi = grid.hi;
@@ -890,10 +889,13 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
for (long b = s.lo; b != 0; b &= b - 1) count[Long.numberOfTrailingZeros(b)]++;
for (long b = s.hi; b != 0; b &= b - 1) count[64 | Long.numberOfTrailingZeros(b)]++;
}
for (int i = 0; i < slots.length; i++) slotScores[i] = slotScore(count, slots[i]);
for (int i = 0; i < slots.length; i++) {
var slot = slots[i];
slotScores[i] = slotScore(count, slot.lo, slot.hi);
}
}
public static FillResult fillMask(Rng rng, Slot[] slots, Grid mask, DictEntry[] dictIndex) {
public static FillResult fillMask(Rng rng, Slot[] slots, Grid mask) {
val multiThreaded = Thread.currentThread().getName().contains("pool");
val NO_LOG = (!Main.VERBOSE || multiThreaded);
val grid = mask;
@@ -943,7 +945,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
var s = slots[i];
if (assigned[s.key] != X) continue;
var pattern = patternForSlot(grid, s.key, s.lo, s.hi);
var index = dictIndex[s.length()];
var index = s.entry;
count = pattern == X ? index.length : candidateCountForPattern(bitset, pattern, index.posBitsets, index.numlong);
if (count == 0) {
@@ -965,7 +967,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
return;
}
var pattern = patternForSlot(grid, best.key, best.lo, best.hi);
var index = dictIndex[best.length()];
var index = best.entry;
current = CARRIER;
current.slot = best;
current.count = index.length;
@@ -992,10 +994,12 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
lastMRV = pick.count;
if (!NO_LOG) renderProgress();
val s = pick.slot;
val k = s.key;
val entry = dictIndex[s.length()];
val s = pick.slot;
val k = s.key;
val slo = s.lo;
val shi = s.hi;
val entry = s.entry;
long low, top;
if (info != null && info.length > 0) {
var idxs = info;
var L = idxs.length;
@@ -1009,9 +1013,9 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
var w = entry.words[idx];
var lemIdx = Lemma.unpackIndex(w);
if (used.get(lemIdx)) continue;
val low = grid.lo;
val top = grid.hi;
if (!placeWord(grid, k, s.lo, s.hi, w)) continue;
low = grid.lo;
top = grid.hi;
if (!placeWord(grid, k, slo, shi, w)) continue;
used.set(lemIdx);
assigned[k] = w;
@@ -1036,9 +1040,9 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
var w = entry.words[idxInArray];
var lemIdx = Lemma.unpackIndex(w);
if (used.get(lemIdx)) continue;
val low = grid.lo;
val top = grid.hi;
if (!placeWord(grid, k, s.lo, s.hi, w)) continue;
low = grid.lo;
top = grid.hi;
if (!placeWord(grid, k, slo, shi, w)) continue;
used.set(lemIdx);
assigned[k] = w;