Gather data

This commit is contained in:
mike
2026-01-09 23:32:58 +01:00
parent a666305b76
commit 5275ac60ff
4 changed files with 60 additions and 58 deletions

View File

@@ -47,7 +47,7 @@ public record Export() {
for (var r = 0; r < R; r++) {
if (r > 0) sb.append('\n');
for (var c = 0; c < C; c++) {
sb.append(grid.isDigitAt(r, c) ? ' ' : (char) grid.byteAt(Grid.offset(r, c)));
sb.append(grid.isDigitAt(Grid.offset(r, c)) ? ' ' : (char) grid.byteAt(Grid.offset(r, c)));
}
}
return sb.toString();
@@ -158,7 +158,7 @@ public record Export() {
for (var r = 0; r < R; r++) {
var sb = new StringBuilder(C);
for (var c = 0; c < C; c++) {
sb.append(g.isLetterAt(r, c) ? (char) g.byteAt(Grid.offset(r, c)) : '#');
sb.append(g.isLetterAt(r,c) ? (char) g.byteAt(Grid.offset(r, c)) : '#');
}
gridv2.add(sb.toString());
}
@@ -188,8 +188,9 @@ public record Export() {
for (var p : placed) {
for (var c : p.cells) {
int rr = Grid.r(c), cc = Grid.c(c);
if (inBounds(rr, cc) && g.isLetterAt(rr, cc)) {
letterAt.put(Bit.pack(rr, cc), (char) g.byteAt(Grid.offset(rr, cc)));
int idx = Grid.offset(rr,cc);
if (inBounds(rr, cc) && g.isLetterAt(idx)) {
letterAt.put(Bit.pack(rr, cc), (char) g.byteAt(idx));
}
}
}

View File

@@ -36,11 +36,11 @@ public record SwedishGenerator(Rng rng) {
@FunctionalInterface interface SlotVisitor { void visit(int key, long packedPos, int len); }
//@formatter:on
static final int BAR_LEN = 22;
static final int C = Config.PUZZLE_COLS;
static final double CROSS_R = (C - 1) / 2.0;
static final int R = Config.PUZZLE_ROWS;
static final double CROSS_C = (R - 1) / 2.0;
static final int SIZE = C * R;// ~18
static final int C = Config.PUZZLE_COLS;
static final double CROSS_R = (C - 1) / 2.0;
static final int R = Config.PUZZLE_ROWS;
static final double CROSS_C = (R - 1) / 2.0;
static final int SIZE = C * R;// ~18
static final double SIZED = (double) SIZE;// ~18
static final int TARGET_CLUES = SIZE >> 2;
static final int MAX_WORD_LENGTH = C <= R ? C : R;
@@ -198,18 +198,17 @@ public record SwedishGenerator(Rng rng) {
record Grid(byte[] g, long[] bo) {
public Grid(byte[] g) { this(g, new long[2]); }
static Grid createEmpty() { return new Grid(new byte[SIZE], new long[2]); }
int digitAt(int r, int c) { return g[offset(r, c)] - 48; }
int digitAt(int index) { return g[index] - 48; }
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); }
Grid deepCopyGrid() { return new Grid(g.clone(), bo.clone()); }
public byte byteAt(int r, int c) { return g[offset(r, c)]; }
public byte byteAt(int pos) { return g[pos]; }
void setCharAt(int r, int c, char ch) { g[offset(r, c)] = (byte) ch; }
void setByteAt(int idx, byte ch) { g[idx] = ch; }
public Grid(byte[] g) { this(g, new long[2]); }
static Grid createEmpty() { return new Grid(new byte[SIZE], new long[2]); }
int digitAt(int r, int c) { return g[offset(r, c)] - 48; }
int digitAt(int index) { return g[index] - 48; }
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); }
Grid deepCopyGrid() { return new Grid(g.clone(), bo.clone()); }
public byte byteAt(int r, int c) { return g[offset(r, c)]; }
public byte byteAt(int pos) { return g[pos]; }
void setByteAt(int idx, byte ch) { g[idx] = ch; }
void setAt(int idx, byte ch) {
if (isDigit(ch)) setClue(idx, ch);
else g[idx] = ch;
@@ -225,18 +224,18 @@ public record SwedishGenerator(Rng rng) {
if (idx < 64) bo[0] &= ~(1L << idx);
else bo[1] &= ~(1L << (idx & 63));
}
public boolean isLetterAt(int r, int c) { return ((g[offset(r, c)] & 64) != 0); }
boolean isDigitAt(int r, int c) { return (g[offset(r, c)] & 48) == 48; }
boolean isDigitAt(int index) { return (g[index] & 48) == 48; }
static boolean isDigit(byte b) { return (b & 48) == 48; }
boolean isLettercell(int r, int c) { return (g[offset(r, c)] & 48) != 48; }
boolean isLetterAt(int index) { return (g[index] & 48) != 48; }
public boolean isLetterAt(int r, int c) { return ((g[offset(r, c)] & 64) != 0); }
public boolean isLetterAt(int index) { return (g[index] & 48) != 48; }
public double similarity(Grid b) {
var same = 0;
for (int i = 0; i < SIZE; i++) if (g[i] == b.g[i]) same++;
return same / SIZED;
}
int clueCount() { return Long.bitCount(bo[0]) + Long.bitCount(bo[1]); }
int clueCount() { return Long.bitCount(bo[0]) + Long.bitCount(bo[1]); }
void forEachSetBit71(IntConsumer consumer) {
for (var lo = bo[0]; lo != 0; lo &= lo - 1) consumer.accept(Long.numberOfTrailingZeros(lo));

View File

@@ -1,5 +1,6 @@
package puzzle;
import lombok.val;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import puzzle.Export.PuzzleResult;
@@ -16,13 +17,13 @@ public class MainTest {
@Test
void testExtractSlots() {
var grid = Grid.createEmpty();
var grid = Grid.createEmpty();
// Set up digits on the grid to create slots.
// '2' (right) at (0,0) -> slot at (0,1), (0,2)
grid.setClue(0, (byte) '2');
grid.setCharAt(0, 1, 'A');
grid.setCharAt(0, 2, 'B');
grid.setByteAt(Grid.offset(0, 1), (byte) 'A');
grid.setByteAt(Grid.offset(0, 2), (byte) 'B');
var slots = extractSlots(grid);
assertEquals(1, slots.size());
@@ -51,7 +52,7 @@ public class MainTest {
@Test
void testForEachSlot() {
var grid = Grid.createEmpty();
var grid = Grid.createEmpty();
grid.setClue(0, (byte) '2'); // right
var count = new AtomicInteger(0);
@@ -76,14 +77,14 @@ public class MainTest {
var grid = Grid.createEmpty();
// Test set/get
grid.setCharAt(0, 0, 'A');
grid.setCharAt(1, 2, '5');
grid.setCharAt(2, 3, 'Z');
grid.setByteAt(Grid.offset(0, 0), (byte) 'A');
grid.setClue(Grid.offset(1, 2), (byte) '5');
grid.setByteAt(Grid.offset(2, 3), (byte) 'Z');
Assertions.assertEquals((byte) 'A', grid.byteAt(0, 0));
Assertions.assertEquals((byte) '5', grid.byteAt(1, 2));
Assertions.assertEquals((byte) 'Z', grid.byteAt(2, 3));
Assertions.assertEquals(DASH, grid.byteAt(1, 1));
Assertions.assertEquals((byte) 'A', grid.byteAt(Grid.offset(0, 0)));
Assertions.assertEquals((byte) '5', grid.byteAt(Grid.offset(1, 2)));
Assertions.assertEquals((byte) 'Z', grid.byteAt(Grid.offset(2, 3)));
Assertions.assertEquals(DASH, grid.byteAt(Grid.offset(1, 1)));
// Test isLetterAt
Assertions.assertTrue(grid.isLetterAt(0, 0));
@@ -92,11 +93,11 @@ public class MainTest {
Assertions.assertFalse(grid.isLetterAt(1, 1));
// Test isDigitAt
Assertions.assertFalse(grid.isDigitAt(0, 0));
Assertions.assertTrue(grid.isDigitAt(1, 2));
Assertions.assertEquals(5, grid.digitAt(1, 2));
Assertions.assertFalse(grid.isDigitAt(2, 3));
Assertions.assertFalse(grid.isDigitAt(1, 1));
Assertions.assertFalse(grid.isDigitAt(0));
Assertions.assertTrue(grid.isDigitAt(Grid.offset(1, 2)));
Assertions.assertEquals(5, grid.digitAt(Grid.offset(1, 2)));
Assertions.assertFalse(grid.isDigitAt(Grid.offset(2, 3)));
Assertions.assertFalse(grid.isDigitAt(Grid.offset(1, 1)));
// Test isLettercell
Assertions.assertTrue(grid.isLettercell(0, 0)); // 'A' is letter
@@ -107,24 +108,25 @@ public class MainTest {
@Test
public void testGridDeepCopy() {
var grid = Grid.createEmpty();
grid.setCharAt(0, 0, 'A');
grid.setCharAt(0, 1, 'B');
grid.setCharAt(1, 0, 'C');
grid.setCharAt(1, 1, 'D');
grid.setByteAt(Grid.offset(0, 0), (byte) 'A');
grid.setByteAt(Grid.offset(0, 1), (byte) 'B');
grid.setByteAt(Grid.offset(1, 0), (byte) 'C');
grid.setByteAt(Grid.offset(1, 1), (byte) 'D');
var copy = grid.deepCopyGrid();
Assertions.assertEquals((byte) 'A', copy.byteAt(0, 0));
Assertions.assertEquals((byte) 'A', copy.byteAt(0));
copy.setCharAt(0, 0, 'X');
Assertions.assertEquals((byte) 'X', copy.byteAt(0, 0));
Assertions.assertEquals((byte) 'A', grid.byteAt(0, 0)); // Original should be unchanged
copy.setByteAt(0, (byte) 'X');
Assertions.assertEquals((byte) 'X', copy.byteAt(0));
Assertions.assertEquals((byte) 'A', grid.byteAt(0)); // Original should be unchanged
}
@Test
public void testMini() {
var grid = Grid.createEmpty();
grid.setCharAt(1, 1, '1');
Assertions.assertTrue(grid.isDigitAt(1, 1));
val idx = Grid.offset(1, 1);
grid.setClue(idx, (byte) '1');
Assertions.assertTrue(grid.isDigitAt(idx));
}
@Test
public void testAttempt() {

View File

@@ -70,21 +70,21 @@ public class SwedishGeneratorTest {
@Test
void testGrid() {
var grid = Grid.createEmpty();
grid.setCharAt(0, 0, 'A');
grid.setCharAt(0, 1, '1');
grid.setByteAt(0, (byte) 'A');
grid.setClue(Grid.offset(0, 1), (byte) '1');
assertEquals('A', grid.byteAt(0, 0));
assertEquals(1, grid.digitAt(0, 1));
assertTrue(grid.isLetterAt(0, 0));
assertFalse(grid.isDigitAt(0, 0));
assertTrue(grid.isDigitAt(0, 1));
assertFalse(grid.isLetterAt(0, 1));
assertTrue(grid.isLetterAt(0));
assertFalse(grid.isDigitAt(0));
assertTrue(grid.isDigitAt(Grid.offset(0, 1)));
assertFalse(grid.isLetterAt(Grid.offset(0, 1)));
assertTrue(grid.isLettercell(0, 0));
assertFalse(grid.isLettercell(0, 1));
var copy = grid.deepCopyGrid();
assertEquals('A', copy.byteAt(0, 0));
copy.setCharAt(0, 0, 'B');
copy.setByteAt(0, (byte) 'B');
assertEquals('B', copy.byteAt(0, 0));
assertEquals('A', grid.byteAt(0, 0));
}
@@ -298,7 +298,7 @@ public class SwedishGeneratorTest {
// 4. Partial placement then conflict (rollback)
grid = Grid.createEmpty();
grid.setCharAt(0, 2, 'X'); // Conflict at the end
grid.setByteAt(Grid.offset(0, 2), (byte) 'X'); // Conflict at the end
assertFalse(placeWord(grid, s, w1, undoBuffer, 3));
// Verify grid is still empty (except for 'X')
assertEquals(DASH, grid.byteAt(0, 0));