diff --git a/src/main/java/precomp/Mask.java b/src/main/java/precomp/Mask.java index 4ffcd23..6e5986b 100644 --- a/src/main/java/precomp/Mask.java +++ b/src/main/java/precomp/Mask.java @@ -17,5 +17,12 @@ public sealed interface Mask int place(); byte d(); default byte letter() { return (byte) (d() | 64); } - default byte clueChar() { return (byte) (d() | 48); } + default void letter(byte[] template, int minR, int minC, int height, int width) { + int rr = r() - minR; + int cc = c() - minC; + if (rr >= 0 && rr < height && cc >= 0 && cc < width) { + template[rr * (width + 1) + cc] = letter(); + } + } + default byte clueChar() { return (byte) (d() | 48); } } \ No newline at end of file diff --git a/src/main/java/puzzle/Export.java b/src/main/java/puzzle/Export.java index 153cddc..86b46fe 100644 --- a/src/main/java/puzzle/Export.java +++ b/src/main/java/puzzle/Export.java @@ -33,8 +33,6 @@ public record Export() { @Shaped static final byte SPACE = Const9x8.SPACE; @Shaped static final byte LINE_BREAK = Const9x8.LINE_BREAK; @Shaped static final byte DASH = Const9x8.DASH; - @Shaped static final int R = Const9x8.R; - @Shaped static final int C = Const9x8.C; @Shaped static final int SIZE = Const9x8.SIZE; @Shaped static final byte[] INIT_GRID_OUTPUT_ARR = Const9x8.INIT_GRID_OUTPUT_ARR; @Shaped static final byte[] INIT_GRID_OUTPUT_DASH_ARR = Const9x8.INIT_GRID_OUTPUT_DASH_ARR; @@ -44,19 +42,14 @@ public record Export() { public static final ThreadLocal BYTES = ThreadLocal.withInitial( () -> new ExportTemplates(INIT_GRID_OUTPUT_ARR, INIT_GRID_OUTPUT_DASH_ARR, new byte[8])); - static int HI(int in) { return in | 64; } - static byte LETTER(int in) { return (byte) (in | 64); } - static byte CLUE_CHAR(int s) { return (byte) (s | 48); } - static int INDEX_ROW(int idx) { return idx % R; } - static int INDEX_COL(int idx) { return idx / R; } - static int INDEX(int idx, int cols) { return INDEX_ROW(idx) * cols + INDEX_COL(idx); } + static int HI(int in) { return in | 64; } public static String gridToString(Clues clues) { val chars = BYTES.get().table(); - var signa = new Signa(clues); - signa.forEach(v -> chars[INDEX(v.index(), C + 1)] = CLUE_CHAR(v.clue())); + var signa = new Signa(clues).map(v -> CELLS[v.cellIndex()]).toArray(Mask[]::new); + Arrays.stream(signa).forEach(v -> chars[v.place()] = v.clueChar()); val result = new String(chars); - signa.forEach(v -> chars[INDEX(v.index(), C + 1)] = SPACE); + Arrays.stream(signa).forEach(v -> chars[v.place()] = SPACE); return result; } @@ -66,7 +59,7 @@ public record Export() { public Puzzle { for (var l = grid.lo & MASK_LO & ~cl.lo; l != X; l &= l - 1) set(Long.numberOfTrailingZeros(l), cells, grid.g); for (var h = grid.hi & MASK_HI & ~cl.hi; h != X; h &= h - 1) set(HI(Long.numberOfTrailingZeros(h)), cells, grid.g); - new Signa(cl).forEach(v -> cells[v.index()] = CELLS[v.index() * 33 + 27 + Slot.dir(v.clue())]); + new Signa(cl).forEach(v -> cells[v.index()] = CELLS[v.cellIndex()]); } static void set(int idx, Mask[] cells, byte[] read) { cells[idx] = CELLS[idx * 33 + read[idx]]; } public Puzzle(Grid grid, Clues cl) { this(grid, new Mask[grid.g.length], cl); } @@ -81,7 +74,7 @@ public record Export() { public Puzzle sync() { for (var l = grid.lo & MASK_LO & ~cl.lo; l != X; l &= l - 1) set(Long.numberOfTrailingZeros(l), cells, grid.g); for (var h = grid.hi & MASK_HI & ~cl.hi; h != X; h &= h - 1) set(HI(Long.numberOfTrailingZeros(h)), cells, grid.g); - new Signa(cl).forEach(v -> cells[v.index()] = CELLS[v.index() * 33 + 27 + Slot.dir(v.clue())]); + new Signa(cl).forEach(v -> cells[v.index()] = CELLS[v.cellIndex()]); return this; } } @@ -128,13 +121,7 @@ public record Export() { Arrays.fill(template, DASH); for (int i = width; i < template.length; i += width + 1) template[i] = LINE_BREAK; - puzzle.forEach(l -> { - int rr = l.r() - MINR; - int cc = l.c() - MINC; - if (rr >= 0 && rr < height && cc >= 0 && cc < width) { - template[rr * (width + 1) + cc] = l.letter(); - } - }); + puzzle.forEach(l -> l.letter(template, MINR, MINC, height, width)); var grid = new String(template).split("\n"); // 5) words output with cropped coordinates diff --git a/src/main/java/puzzle/Riddle.java b/src/main/java/puzzle/Riddle.java index 1c719bc..866c50f 100644 --- a/src/main/java/puzzle/Riddle.java +++ b/src/main/java/puzzle/Riddle.java @@ -86,7 +86,12 @@ public class Riddle { public static Clue from(int dir) { return CLUES[dir]; } } - public record Vestigium(int index, int clue) { } + public record Vestigium(int index, int clue) { + + public int cellIndex() { + return index * 33 + 27 + Slot.dir(clue); + } + } public record ExportedPuzzle(String[] grid, WordOut[] words, int difficulty, Rewards rewards) { }