redo
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
package puzzle;
|
||||
|
||||
import module java.base;
|
||||
import anno.GenerateShapedCopies;
|
||||
import anno.Shaped;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.experimental.Delegate;
|
||||
import lombok.val;
|
||||
import precomp.Const9x8;
|
||||
import puzzle.Export.Puzzle.Replacar.Rell;
|
||||
import puzzle.Meta.ShardLem;
|
||||
import puzzle.SwedishGenerator.Dict;
|
||||
import puzzle.Replacar.Rell;
|
||||
import puzzle.SwedishGenerator.FillResult;
|
||||
import puzzle.SwedishGenerator.Grid;
|
||||
import puzzle.SwedishGenerator.Slotinfo;
|
||||
@@ -26,26 +26,21 @@ import static puzzle.Masker.Slot;
|
||||
import static puzzle.SwedishGenerator.Lemma;
|
||||
import static puzzle.SwedishGenerator.X;
|
||||
|
||||
/**
|
||||
* ExportFormat.java
|
||||
*
|
||||
* Direct port of export_format.js:
|
||||
* - scans filled grid for clue digits '1'..'4'
|
||||
* - extracts placed words in canonical direction (horizontal=right, vertical=down)
|
||||
* - crops to bounding box (words + arrow cells) with 1-cell margin
|
||||
* - outputs gridv2 + words[] (+ difficulty, rewards)
|
||||
*/
|
||||
@GenerateShapedCopies(
|
||||
packageName = "puzzle",
|
||||
className = "ExportX",
|
||||
shapes = { "precomp.Const9x8", "precomp.Const3x4" }
|
||||
)
|
||||
public record Export() {
|
||||
|
||||
public static final ThreadLocal<byte[]> BYTES = ThreadLocal.withInitial(() -> new byte[8]);
|
||||
static final byte CLUE_UP = 2;
|
||||
static final byte CLUE_LEFT = 3;
|
||||
static final byte CLUE_LEFT_TOP = 4;
|
||||
static final byte CLUE_RIGHT_TOP = 5;
|
||||
@Shaped static final String INIT_GRID_OUTPUT = Const9x8.INIT_GRID_OUTPUT;
|
||||
@Shaped static final int R = Const9x8.R;
|
||||
@Shaped static final int C = Const9x8.C;
|
||||
@Shaped static final byte[] INIT_GRID_OUTPUT_ARR = Const9x8.INIT_GRID_OUTPUT_ARR;
|
||||
@Shaped static final long MASK_HI = Const9x8.MASK_HI;
|
||||
@Shaped static final long MASK_LO = Const9x8.MASK_LO;
|
||||
|
||||
static int HI(int in) { return in | 64; }
|
||||
static char LETTER(int in) { return (char) (in | 64); }
|
||||
static char CLUE_CHAR(int s) { return (char) (s | 48); }
|
||||
@@ -79,34 +74,27 @@ public record Export() {
|
||||
public Signa deepCopyGrid() { return new Signa(new Clues(c.lo, c.hi, c.vlo, c.vhi, c.rlo, c.rhi, c.xlo, c.xhi)); }
|
||||
String gridToString() {
|
||||
var sb = new StringBuilder(INIT_GRID_OUTPUT);
|
||||
Masker.forEachSlot(c, (s, _, _) -> {
|
||||
val idx = Slot.clueIndex(s);
|
||||
val dir = Slot.dir(s);
|
||||
sb.setCharAt(INDEX(INDEX_ROW(idx), C + 1, INDEX_COL(idx)), CLUE_CHAR(dir));
|
||||
});
|
||||
stream().forEach(v -> sb.setCharAt(INDEX(INDEX_ROW(v.index), C + 1, INDEX_COL(v.index)), CLUE_CHAR(v.clue)));
|
||||
return sb.toString();
|
||||
}
|
||||
public Stream<Vestigium> stream() {
|
||||
val stream = Stream.<Vestigium>builder();
|
||||
for (var l = c.lo & ~c.xlo & ~c.rlo & c.vlo; l != X; l &= l - 1) stream.accept(new Vestigium(Long.numberOfTrailingZeros(l), RIGHT1.dir));
|
||||
for (var l = c.lo & ~c.xlo & ~c.rlo & ~c.vlo; l != X; l &= l - 1) stream.accept(new Vestigium(Long.numberOfTrailingZeros(l), DOWN0.dir));
|
||||
for (var l = c.lo & ~c.xlo & c.rlo & ~c.vlo; l != X; l &= l - 1) stream.accept(new Vestigium(Long.numberOfTrailingZeros(l), CLUE_UP));
|
||||
for (var l = c.lo & ~c.xlo & c.rlo & c.vlo; l != X; l &= l - 1) stream.accept(new Vestigium(Long.numberOfTrailingZeros(l), CLUE_LEFT));
|
||||
for (var l = c.lo & c.xlo & ~c.rlo & ~c.vlo; l != X; l &= l - 1) stream.accept(new Vestigium(Long.numberOfTrailingZeros(l), CLUE_LEFT_TOP));
|
||||
for (var l = c.lo & c.xlo & ~c.rlo & c.vlo; l != X; l &= l - 1) stream.accept(new Vestigium(Long.numberOfTrailingZeros(l), CLUE_RIGHT_TOP));
|
||||
for (var l = c.lo & ~c.xlo & c.rlo & ~c.vlo; l != X; l &= l - 1) stream.accept(new Vestigium(Long.numberOfTrailingZeros(l), CLUE_UP2));
|
||||
for (var l = c.lo & ~c.xlo & c.rlo & c.vlo; l != X; l &= l - 1) stream.accept(new Vestigium(Long.numberOfTrailingZeros(l), CLUE_LEFT3));
|
||||
for (var l = c.lo & c.xlo & ~c.rlo & ~c.vlo; l != X; l &= l - 1) stream.accept(new Vestigium(Long.numberOfTrailingZeros(l), CLUE_LEFT_TOP4));
|
||||
for (var l = c.lo & c.xlo & ~c.rlo & c.vlo; l != X; l &= l - 1) stream.accept(new Vestigium(Long.numberOfTrailingZeros(l), CLUE_RIGHT_TOP5));
|
||||
|
||||
for (var h = c.hi & ~c.xhi & ~c.rhi & c.vhi; h != X; h &= h - 1) stream.accept(new Vestigium(HI(Long.numberOfTrailingZeros(h)), CLUE_RIGHT1));
|
||||
for (var h = c.hi & ~c.xhi & ~c.rhi & ~c.vhi; h != X; h &= h - 1) stream.accept(new Vestigium(HI(Long.numberOfTrailingZeros(h)), CLUE_DOWN0));
|
||||
for (var h = c.hi & ~c.xhi & c.rhi & ~c.vhi; h != X; h &= h - 1) stream.accept(new Vestigium(HI(Long.numberOfTrailingZeros(h)), CLUE_UP));
|
||||
for (var h = c.hi & ~c.xhi & c.rhi & c.vhi; h != X; h &= h - 1) stream.accept(new Vestigium(HI(Long.numberOfTrailingZeros(h)), CLUE_LEFT));
|
||||
for (var h = c.hi & c.xhi & ~c.rhi & ~c.vhi; h != X; h &= h - 1) stream.accept(new Vestigium(HI(Long.numberOfTrailingZeros(h)), CLUE_LEFT_TOP));
|
||||
for (var h = c.hi & c.xhi & ~c.rhi & c.vhi; h != X; h &= h - 1) stream.accept(new Vestigium(HI(Long.numberOfTrailingZeros(h)), CLUE_RIGHT_TOP));
|
||||
for (var h = c.hi & ~c.xhi & c.rhi & ~c.vhi; h != X; h &= h - 1) stream.accept(new Vestigium(HI(Long.numberOfTrailingZeros(h)), CLUE_UP2));
|
||||
for (var h = c.hi & ~c.xhi & c.rhi & c.vhi; h != X; h &= h - 1) stream.accept(new Vestigium(HI(Long.numberOfTrailingZeros(h)), CLUE_LEFT3));
|
||||
for (var h = c.hi & c.xhi & ~c.rhi & ~c.vhi; h != X; h &= h - 1) stream.accept(new Vestigium(HI(Long.numberOfTrailingZeros(h)), CLUE_LEFT_TOP4));
|
||||
for (var h = c.hi & c.xhi & ~c.rhi & c.vhi; h != X; h &= h - 1) stream.accept(new Vestigium(HI(Long.numberOfTrailingZeros(h)), CLUE_RIGHT_TOP5));
|
||||
|
||||
return stream.build();
|
||||
}
|
||||
public Slotinfo[] slots(Dict D) {
|
||||
return Masker.slots(c, D.index(), D.reversed());
|
||||
}
|
||||
}
|
||||
|
||||
record Puzzle(@Delegate Grid grid, Clues cl)
|
||||
@@ -116,31 +104,19 @@ public record Export() {
|
||||
public Puzzle(Signa clues) { this(clues.c); }
|
||||
public @Delegate Stream<Lettrix> stream() {
|
||||
val stream = Stream.<Lettrix>builder();
|
||||
for (var l = grid.lo & ~cl.lo; l != X; l &= l - 1) stream.accept(Lettrix.from(Long.numberOfTrailingZeros(l), grid.g));
|
||||
for (var h = grid.hi & ~cl.hi & 0xFF; h != X; h &= h - 1) stream.accept(Lettrix.from(64 | Long.numberOfTrailingZeros(h), grid.g));
|
||||
for (var l = grid.lo & MASK_LO & ~cl.lo; l != X; l &= l - 1) stream.accept(Lettrix.from(Long.numberOfTrailingZeros(l), grid.g));
|
||||
for (var h = grid.hi & MASK_HI & ~cl.hi; h != X; h &= h - 1) stream.accept(Lettrix.from(64 | Long.numberOfTrailingZeros(h), grid.g));
|
||||
return stream.build();
|
||||
}
|
||||
String gridToString(Slotinfo[] slots) {
|
||||
public String[] exportGrid(Slotinfo[] slots, Replacar clueChar, char emptyFallback) {
|
||||
var sb = INIT_GRID_OUTPUT_ARR.clone();
|
||||
for (var slot : slots) {
|
||||
val idx = Slot.clueIndex(slot.key());
|
||||
val r = idx & 7;
|
||||
val c = idx >>> 3;
|
||||
val r = INDEX_ROW(idx);
|
||||
val c = INDEX_COL(idx);
|
||||
val dir = Slot.dir(slot.key());
|
||||
sb[r * (C + 1) + c] = (byte) (dir | 48);
|
||||
}
|
||||
stream().forEach((l) -> sb[l.index(C + 1)] = (byte) l.human());
|
||||
return new String(sb);
|
||||
}
|
||||
public String[] exportGrid(Replacar clueChar, char emptyFallback) {
|
||||
var sb = INIT_GRID_OUTPUT_ARR.clone();
|
||||
Masker.forEachSlot(cl, (s, l, a) -> {
|
||||
val idx = Slot.clueIndex(s);
|
||||
val r = idx & 7;
|
||||
val c = idx >>> 3;
|
||||
val dir = Slot.dir(s);
|
||||
sb[r * (C + 1) + c] = (byte) clueChar.replace(new Rell(grid, cl, idx, (byte) (dir | 48)));
|
||||
});
|
||||
}
|
||||
stream().forEach((l) -> sb[l.index(C + 1)] = (byte) l.human());
|
||||
return new String(sb).replaceAll(" ", String.valueOf(emptyFallback)).split("\n");
|
||||
}
|
||||
@@ -192,13 +168,6 @@ public record Export() {
|
||||
}
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
interface Replacar {
|
||||
|
||||
record Rell(Grid grid, Clues clues, int index, byte data) { }
|
||||
char replace(Rell c);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
record Placed(long lemma, int slotKey, rci[] cells) {
|
||||
@@ -253,16 +222,28 @@ public record Export() {
|
||||
|
||||
public record PuzzleResult(Signa clues, Puzzle grid, Slotinfo[] slots, FillResult filled) {
|
||||
|
||||
public String cluesGridToString() {
|
||||
return clues.gridToString();
|
||||
}
|
||||
public String gridRenderHuman() {
|
||||
return String.join("\n", grid.exportGrid(_ -> ' ', '#'));
|
||||
return String.join("\n", grid.exportGrid(slots, _ -> ' ', '#'));
|
||||
}
|
||||
public String gridGridToString() {
|
||||
return grid.gridToString(slots);
|
||||
var sb = INIT_GRID_OUTPUT_ARR.clone();
|
||||
for (var slot : slots) {
|
||||
val idx = Slot.clueIndex(slot.key());
|
||||
val r = INDEX_ROW(idx);
|
||||
val c = INDEX_COL(idx);
|
||||
val dir = Slot.dir(slot.key());
|
||||
sb[r * (C + 1) + c] = (byte) (dir | 48);
|
||||
}
|
||||
grid.stream().forEach((l) -> sb[l.index(C + 1)] = (byte) l.human());
|
||||
return new String(sb);
|
||||
}
|
||||
public ExportedPuzzle exportFormatFromFilled(Rewards rewards, rci[] rcis, int bits) {
|
||||
public ExportedPuzzle exportFormatFromFilled(Rewards rewards, rci[] rcis) {
|
||||
// If nothing placed: return full grid mapped to letters/# only
|
||||
if (slots.length == 0) {
|
||||
return new ExportedPuzzle(grid.exportGrid(_ -> '#', '#'), new WordOut[0], 1, rewards);
|
||||
return new ExportedPuzzle(grid.exportGrid(slots, _ -> '#', '#'), new WordOut[0], 1, rewards);
|
||||
}
|
||||
|
||||
var placed = Arrays.stream(slots)
|
||||
@@ -281,19 +262,19 @@ public record Export() {
|
||||
maxC = Math.max(maxC, it.c());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 3) map of only used letter cells (everything else becomes '#')
|
||||
var map = grid.stream().collect(Collectors.toMap(Lettrix::index, Lettrix::human));
|
||||
// 4) render gridv2 over cropped bounds (out-of-bounds become '#')
|
||||
var gridv2 = new String[Math.max(0, maxR - minR + 1)];
|
||||
for (int r = minR, i = 0; r <= maxR; r++, i++) {
|
||||
var row = new StringBuilder(Math.max(0, maxC - minC + 1));
|
||||
for (var c = minC; c <= maxC; c++) row.append(map.getOrDefault(r | (c << bits), '#'));
|
||||
for (var c = minC; c <= maxC; c++) row.append(map.getOrDefault(r | (c << 3), '#'));
|
||||
gridv2[i] = row.toString();
|
||||
}
|
||||
|
||||
// 5) words output with cropped coordinates
|
||||
int MIN_R = minR, MIN_C = minC;
|
||||
|
||||
val bytes = BYTES.get();
|
||||
var wordsOut = Arrays.stream(placed).map(p -> new WordOut(
|
||||
p.lemma,
|
||||
@@ -308,7 +289,6 @@ public record Export() {
|
||||
return new ExportedPuzzle(gridv2, wordsOut, (int) (total / wordsOut.length), rewards);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
record Lettrix(int index, byte letter) {
|
||||
|
||||
Reference in New Issue
Block a user