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.experimental.Accessors;
import lombok.experimental.Delegate;
import puzzle.Export.Gridded;
import puzzle.SwedishGenerator.Dict;
import puzzle.SwedishGenerator.FillResult;
import puzzle.SwedishGenerator.Grid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.StringJoiner;
import static puzzle.SwedishGenerator.R;
import static puzzle.SwedishGenerator.Lemma;
import static puzzle.SwedishGenerator.Slot;
@@ -35,6 +37,9 @@ public record Export() {
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() {
var sb = new StringBuilder();
for (var r = 0; r < R; r++) {
@@ -44,14 +49,22 @@ public record Export() {
return sb.toString();
}
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++) {
if (r > 0) sb.append('\n');
var sb = new StringBuilder(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));
}
}
out[r] = sb.toString();
}
return sb.toString();
return out;
}
}
@@ -114,16 +127,16 @@ public record Export() {
for (int i = 0, len = s.len(); i < len; i++) cells[i] = s.pos(i);
char direction;
var startRow = Grid.r(cells[0]);
var startCol = Grid.c(cells[0]);
var startRow = Grid.r(cells[0]);
var startCol = Grid.c(cells[0]);
if (d == 2) { // right -> horizontal
direction = Placed.HORIZONTAL;
} else if (d == 3 || d == 5) { // down or down-bent -> vertical
direction = Placed.VERTICAL;
} else if (d == 4) { // left -> horizontal (REVERSED)
direction = Placed.HORIZONTAL;
direction = Placed.HORIZONTAL;
} else if (d == 1) { // up -> vertical (REVERSED)
direction = Placed.VERTICAL;
direction = Placed.VERTICAL;
} else {
return null;
}
@@ -153,16 +166,7 @@ public record Export() {
// If nothing placed: return full grid mapped to letters/# only
if (placed.isEmpty()) {
var gridv2 = new String[R];
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);
return new ExportedPuzzle(g.exportGrid('#', '#'), new WordOut[0], difficulty, rewards);
}
// 2) bounding box around all word cells + arrow cells, with 1-cell margin