diff --git a/src/main/java/puzzle/SwedishGenerator.java b/src/main/java/puzzle/SwedishGenerator.java index 18a4325..3048d3a 100644 --- a/src/main/java/puzzle/SwedishGenerator.java +++ b/src/main/java/puzzle/SwedishGenerator.java @@ -72,10 +72,8 @@ public record SwedishGenerator(Rng rng) { static final byte B64 = (byte) 64; static final byte B48 = (byte) 48; // Directions for '1'..'6' - static final nbrs_16[] OFFSETS = Neighbors9x8.OFFSETS; + static final long[] OFFSETS_D_IDX = Neighbors9x8.OFFSET_D_IDX; static final nbrs_16[] OFFSETS_FOUR = Neighbors9x8.OFFSETS_FOUR; - static final nbrs_8[] nbrs8 = Neighbors9x8.nbrs8; - static final nbrs_8[] nbrs4 = Neighbors9x8.nbrs4; static final rci[] IT = Neighbors9x8.IT; static final long[] INBR8_PACKEDT = Neighbors9x8.NBR8_PACKED; static final int[][] MUTATE_RI = new int[SIZE][625]; @@ -175,7 +173,7 @@ public record SwedishGenerator(Rng rng) { this.hi = hi; } static Grid createEmpty() { return new Grid(new byte[SIZE], X, X); } - int digitAt(int index) { return g[index] & ~48; } + int digitAt(int index) { return g[index] & 7; } public static int r(int offset) { return offset & 7; } public static int c(int offset) { return offset >>> 3; } static int offset(int r, int c) { return r | (c << 3); } @@ -266,7 +264,7 @@ public record SwedishGenerator(Rng rng) { int intAt(int idx) { return (int) (((word >>> (idx * 5))) & 0b11111); }// word[]; } @Override public int hashCode() { return unpackIndex(word); } @Override public boolean equals(Object o) { return (o == this) || (o instanceof Lemma l && l.word == word); } - static String[] clue(long w) { return CsvIndexService.clues(unpackIndex(w)); } + static String[] clue(long w) { return CsvIndexService.clues(unpackIndex(w)); } static int simpel(long w) { return CsvIndexService.simpel(unpackIndex(w)); } int length() { return Lemma.length(word); } static int length(long word) { @@ -344,7 +342,6 @@ public record SwedishGenerator(Rng rng) { } } - static record Slot(int key, long packedPos) { static final int BIT_FOR_DIR = 3; @@ -364,7 +361,7 @@ public record SwedishGenerator(Rng rng) { private static void processSlot(Grid grid, SlotVisitor visitor, int idx) { var d = grid.digitAt(idx); - var packed = OFFSETS[d].path()[idx]; + var packed = OFFSETS_D_IDX[(d - 1) | (idx << 2)]; long packedPos = 0L; int k = 0; for (long n = (packed >>> 56), offset = 0L, iidx; k < n; k++, offset += 7L) { @@ -399,10 +396,11 @@ public record SwedishGenerator(Rng rng) { for (long bits = (i == 0 ? lo_cl : hi_cl); bits != X; bits &= bits - 1) { int clueIdx = i | Long.numberOfTrailingZeros(bits); var d = grid.digitAt(clueIdx); - var nbrs16 = OFFSETS[d]; - long packed = nbrs16.path()[clueIdx]; - int n = (int) (packed >>> 56) * 7, k, idx; - var horiz = Slot.horiz(d) ? covH : covV; + long packed = OFFSETS_D_IDX[(d - 1) | (clueIdx << 2)]; + //if (OFFSETS_D_IDX[(d-1) * SIZE + clueIdx] != packed) throw new IllegalStateException("Invalid path index for digit " + d + " at clue " + clueIdx); + + int n = (int) (packed >>> 56) * 7, k, idx; + var horiz = Slot.horiz(d) ? covH : covV; for (k = 0; k < n; k += 7) { idx = (int) ((packed >>> (k)) & 0x7F); if (grid.isClue(idx)) break; @@ -554,7 +552,7 @@ public record SwedishGenerator(Rng rng) { for (var hi = out.hi; hi != X; hi &= hi - 1L) clearClues(out, 64 + Long.numberOfTrailingZeros(hi)); return out; } - public static void clearClues(Grid out, int idx) { if (!out.hasRoomForClue(OFFSETS[out.digitAt(idx)].path()[idx])) out.clearClue(idx); } + public static void clearClues(Grid out, int idx) { if (!out.hasRoomForClue(OFFSETS_D_IDX[(out.digitAt(idx) - 1) | (idx << 2)])) out.clearClue(idx); } Grid hillclimb(Grid start, int limit) { var best = start; diff --git a/src/test/java/puzzle/SwedishGeneratorTest.java b/src/test/java/puzzle/SwedishGeneratorTest.java index e6b6a26..f8d6701 100644 --- a/src/test/java/puzzle/SwedishGeneratorTest.java +++ b/src/test/java/puzzle/SwedishGeneratorTest.java @@ -11,10 +11,11 @@ import static puzzle.SwedishGenerator.*; public class SwedishGeneratorTest { + static final byte D_BYTE_2 = 50; @Test void testPatternForSlotAllLetters() { - var grid = new Grid(new byte[]{ 65, 66, 67 }); // A B C - var slot = Slot.from(18, ((long) 0) | ((long) 1 << 7) | ((long) 2 << 14), 3); + var grid = new Grid(new byte[]{ 65, 66, 67 }); // A B C + var slot = Slot.from(18, ((long) 0) | ((long) 1 << 7) | ((long) 2 << 14), 3); long pattern = patternForSlot(grid, slot); assertEquals(1 | (2 << 5) | (3 << 10), pattern); @@ -22,8 +23,8 @@ public class SwedishGeneratorTest { @Test void testPatternForSlotMixed() { - var grid = new Grid(new byte[]{ 65, DASH, 67 }); // A - C - var slot = Slot.from(1 << 4 | 2, ((long) 0) | ((long) 1 << 7) | ((long) 2 << 14), 3); + var grid = new Grid(new byte[]{ 65, DASH, 67 }); // A - C + var slot = Slot.from(1 << 4 | 2, ((long) 0) | ((long) 1 << 7) | ((long) 2 << 14), 3); long pattern = patternForSlot(grid, slot); assertEquals(1 | (0 << 5) | (3 << 10), pattern); @@ -31,8 +32,8 @@ public class SwedishGeneratorTest { @Test void testPatternForSlotAllDashes() { - var grid = new Grid(new byte[]{ DASH, DASH, DASH }); // - - - - var slot = Slot.from(1 << 4 | 2, ((long) 0) | ((long) 1 << 7) | ((long) 2 << 14), 3); + var grid = new Grid(new byte[]{ DASH, DASH, DASH }); // - - - + var slot = Slot.from(1 << 4 | 2, ((long) 0) | ((long) 1 << 7) | ((long) 2 << 14), 3); long pattern = patternForSlot(grid, slot); assertEquals(0, pattern); @@ -40,8 +41,8 @@ public class SwedishGeneratorTest { @Test void testPatternForSlotSingleLetter() { - var grid = new Grid(new byte[]{ 65, DASH, DASH }); // A - - - var slot = Slot.from(1 << 4 | 2, ((long) 0) | ((long) 1 << 7) | ((long) 2 << 14), 3); + var grid = new Grid(new byte[]{ 65, DASH, DASH }); // A - - + var slot = Slot.from(1 << 4 | 2, ((long) 0) | ((long) 1 << 7) | ((long) 2 << 14), 3); long pattern = patternForSlot(grid, slot); assertEquals(1, pattern); @@ -135,7 +136,7 @@ public class SwedishGeneratorTest { // key = (r << 8) | (c << 4) | d var offset = Grid.offset(2, 3); System.out.println("[DEBUG_LOG] Grid.offset(2, 3) = " + offset); - var key = (offset << Slot.BIT_FOR_DIR) | 5; + var key = (offset << Slot.BIT_FOR_DIR) | 5; System.out.println("[DEBUG_LOG] key = " + key); long packedPos = 0; // pos 0: (2, 5) @@ -273,7 +274,8 @@ public class SwedishGeneratorTest { assertTrue(f1 >= 1_000_000_000L); // Add a slot - grid.setClue(0, OFFSETS[2].dbyte()); + //var dbyte = OFFSETS[2].dbyte(); + grid.setClue(0, D_BYTE_2); var f2 = gen.maskFitness(grid); assertTrue(f2 < f1); }