introduce bitloops

This commit is contained in:
mike
2026-01-12 19:32:05 +01:00
parent c7d67bf778
commit 368473c80f
3 changed files with 27 additions and 22 deletions

View File

@@ -3,12 +3,14 @@ package puzzle;
import lombok.Getter; import lombok.Getter;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
import lombok.experimental.Delegate; import lombok.experimental.Delegate;
import puzzle.Export.Gridded;
import puzzle.SwedishGenerator.Dict; import puzzle.SwedishGenerator.Dict;
import puzzle.SwedishGenerator.FillResult; import puzzle.SwedishGenerator.FillResult;
import puzzle.SwedishGenerator.Grid; import puzzle.SwedishGenerator.Grid;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.StringJoiner;
import static puzzle.SwedishGenerator.R; import static puzzle.SwedishGenerator.R;
import static puzzle.SwedishGenerator.Lemma; import static puzzle.SwedishGenerator.Lemma;
import static puzzle.SwedishGenerator.Slot; import static puzzle.SwedishGenerator.Slot;
@@ -35,6 +37,9 @@ public record Export() {
record Gridded(@Delegate Grid grid) { record Gridded(@Delegate Grid grid) {
static boolean isLetter(byte b) { return (b & SwedishGenerator.B64) != SwedishGenerator.B0; }
//public boolean isLetterSet(int idx) { return isLetter(g[idx]); }
char NOT_CLUE_NOT_LETTER_TO(byte b, char fallback) { return isLetter(b) ? (char) b : fallback; }
String gridToString() { String gridToString() {
var sb = new StringBuilder(); var sb = new StringBuilder();
for (var r = 0; r < R; r++) { for (var r = 0; r < R; r++) {
@@ -44,14 +49,22 @@ public record Export() {
return sb.toString(); return sb.toString();
} }
public String renderHuman() { public String renderHuman() {
var sb = new StringBuilder(); return String.join("\n", exportGrid(' ', '#'));
}
public String[] exportGrid(char clueChar, char emptyFallback) {
var out = new String[R];
for (var r = 0; r < R; r++) { for (var r = 0; r < R; r++) {
if (r > 0) sb.append('\n'); var sb = new StringBuilder(C);
for (var c = 0; c < C; c++) { for (var c = 0; c < C; c++) {
sb.append(grid.isLetterSet(Grid.offset(r, c)) ? (char) grid.byteAt(Grid.offset(r, c)) : ' '); if (grid.isClue(Grid.offset(r, c))) {
sb.append(clueChar);
} else {
sb.append(NOT_CLUE_NOT_LETTER_TO(grid.byteAt(Grid.offset(r, c)), emptyFallback));
} }
} }
return sb.toString(); out[r] = sb.toString();
}
return out;
} }
} }
@@ -153,16 +166,7 @@ public record Export() {
// 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()) {
var gridv2 = new String[R]; return new ExportedPuzzle(g.exportGrid('#', '#'), new WordOut[0], difficulty, rewards);
for (var r = 0; r < R; r++) {
var sb = new StringBuilder(C);
for (var c = 0; c < C; c++) {
int idx = Grid.offset(r, c);
sb.append(g.isLetterSet(idx) ? (char) g.byteAt(idx) : '#');
}
gridv2[r] = sb.toString();
}
return new ExportedPuzzle(gridv2, new WordOut[0], difficulty, rewards);
} }
// 2) bounding box around all word cells + arrow cells, with 1-cell margin // 2) bounding box around all word cells + arrow cells, with 1-cell margin

View File

@@ -58,7 +58,7 @@ public record SwedishGenerator(Rng rng) {
static final char C_DASH = '\0'; static final char C_DASH = '\0';
static final byte _1 = 49, _9 = 57, A = 65, Z = 90, DASH = (byte) C_DASH; static final byte _1 = 49, _9 = 57, A = 65, Z = 90, DASH = (byte) C_DASH;
static final ThreadLocal<Context> CTX = ThreadLocal.withInitial(Context::new); static final ThreadLocal<Context> CTX = ThreadLocal.withInitial(Context::new);
static boolean isLetter(byte b) { return (b & 64) != 0; }
static int clamp(int x, int a, int b) { return Math.max(a, Math.min(b, x)); } static int clamp(int x, int a, int b) { return Math.max(a, Math.min(b, x)); }
record Pick(Slot slot, CandidateInfo info, boolean done) { } record Pick(Slot slot, CandidateInfo info, boolean done) { }
@@ -108,6 +108,7 @@ public record SwedishGenerator(Rng rng) {
stats.simplicity = clueMap.isEmpty() ? 0 : stats.simplicity / clueMap.size(); stats.simplicity = clueMap.isEmpty() ? 0 : stats.simplicity / clueMap.size();
} }
} }
} }
static final class Context { static final class Context {
@@ -185,7 +186,6 @@ public record SwedishGenerator(Rng rng) {
if ((idx & 64) == 0) lo &= ~(1L << idx); if ((idx & 64) == 0) lo &= ~(1L << idx);
else hi &= ~(1L << (idx & 63)); else hi &= ~(1L << (idx & 63));
} }
static boolean isDigit(byte b) { return (b & B48) == B48; }
boolean isClue(long index) { return ((index & 64) == 0) ? ((lo >>> index) & 1L) != X : ((hi >>> (index & 63)) & 1L) != X; } boolean isClue(long index) { return ((index & 64) == 0) ? ((lo >>> index) & 1L) != X : ((hi >>> (index & 63)) & 1L) != X; }
boolean isClue(int index) { return ((index & 64) == 0) ? ((lo >>> index) & 1L) != X : ((hi >>> (index & 63)) & 1L) != X; } boolean isClue(int index) { return ((index & 64) == 0) ? ((lo >>> index) & 1L) != X : ((hi >>> (index & 63)) & 1L) != X; }
boolean notClue(long index) { return ((index & 64) == 0) ? ((lo >>> index) & 1L) == X : ((hi >>> (index & 63)) & 1L) == X; } boolean notClue(long index) { return ((index & 64) == 0) ? ((lo >>> index) & 1L) == X : ((hi >>> (index & 63)) & 1L) == X; }
@@ -572,7 +572,7 @@ public record SwedishGenerator(Rng rng) {
byte ch = b.g[i]; byte ch = b.g[i];
if (out.g[i] != ch) { if (out.g[i] != ch) {
out.g[i] = ch; out.g[i] = ch;
if (Grid.isDigit(ch)) { if (b.isClue(i)) {
if ((i & 64) == 0) bo0 |= (1L << i); if ((i & 64) == 0) bo0 |= (1L << i);
else bo1 |= (1L << (i & 63)); else bo1 |= (1L << (i & 63));
} else { } else {

View File

@@ -164,6 +164,7 @@ public class MainTest {
Assertions.assertEquals(301794542151533187L, res.filled().grid().grid().lo); Assertions.assertEquals(301794542151533187L, res.filled().grid().grid().lo);
Assertions.assertEquals(193L, res.filled().grid().grid().hi); Assertions.assertEquals(193L, res.filled().grid().grid().hi);
} }
boolean isLetter(byte b) { return (b & 64) != 0; }
@Test @Test
public void testIsLetterA() { public void testIsLetterA() {
assertTrue(isLetter((byte) 'A')); assertTrue(isLetter((byte) 'A'));