From 5561cfe62b6df8b3c1a3300d1e3b2cdf024bca5d Mon Sep 17 00:00:00 2001 From: mike Date: Wed, 7 Jan 2026 01:31:42 +0100 Subject: [PATCH] Gather data --- src/main/java/puzzle/SwedishGenerator.java | 48 ++++++++++++---------- src/test/java/puzzle/SlotTest.java | 18 ++++++++ 2 files changed, 44 insertions(+), 22 deletions(-) create mode 100644 src/test/java/puzzle/SlotTest.java diff --git a/src/main/java/puzzle/SwedishGenerator.java b/src/main/java/puzzle/SwedishGenerator.java index 3a96b09..02ffd12 100644 --- a/src/main/java/puzzle/SwedishGenerator.java +++ b/src/main/java/puzzle/SwedishGenerator.java @@ -291,18 +291,26 @@ public record SwedishGenerator(int W, int H, int SIZE, int MAX_LEN, int[] buff) return new CandidateInfo(cur, curLen); } - static record Slot(String key, int clueR, int clueC, int dir, long rs, long cs, int len, boolean horiz) { + static record Slot(int key, long rs, long cs, int len) { public Slot(int clueR, int clueC, int d, long rs, long cs, int len) { - this(clueR + "," + clueC + ":" + d, clueR, clueC, d, rs, cs, len, d == 2 || d == 4); + this((clueR << 8) | (clueC << 4) | d, rs, cs, len); } + public int clueR() { return (key >> 8) & 15; } + public int clueC() { return (key >> 4) & 15; } + public int dir() { return key & 15; } + public boolean horiz() { + return (dir() & 1) == 0; + /* var d = dir(); + return d == 2 || d == 4;*/ + } public int r(int i) { return (int) ((rs >> (i << 2)) & 15); } public int c(int i) { return (int) ((cs >> (i << 2)) & 15); } } ArrayList extractSlots(Grid grid) { - var slots = new ArrayList(); + var slots = new ArrayList(64); for (var r = 0; r < H; r++) { for (var c = 0; c < W; c++) { if (!grid.isDigitAt(r, c)) continue; @@ -364,16 +372,20 @@ public record SwedishGenerator(int W, int H, int SIZE, int MAX_LEN, int[] buff) var covV = new int[H][W]; for (var s : slots) { - if (s.len < MIN_LEN) penalty += 8000; - if (s.len > MAX_LEN) penalty += 8000 + (long) (s.len - MAX_LEN) * 500L; - - if (s.len >= MIN_LEN && s.len <= MAX_LEN) { + if (s.len < MIN_LEN) { + penalty += 8000; + } + /* else if (s.len > MAX_LEN) { + penalty += 8000 + (long) (s.len - MAX_LEN) * 500L; + throw new RuntimeException(); + }*/ + else { if (lenCounts[s.len] <= 0) penalty += 12000; } for (var i = 0; i < s.len; i++) { int r = s.r(i), c = s.c(i); - if (s.horiz) covH[r][c] += 1; + if (s.horiz()) covH[r][c] += 1; else covV[r][c] += 1; } } @@ -597,11 +609,11 @@ public record SwedishGenerator(int W, int H, int SIZE, int MAX_LEN, int[] buff) public static final record FillResult(boolean ok, Grid grid, - HashMap clueMap, + HashMap clueMap, FillStats stats, double simplicity) { - public FillResult(boolean ok, Grid grid, HashMap assigned, FillStats stats) { + public FillResult(boolean ok, Grid grid, HashMap assigned, FillStats stats) { double totalSimplicity = 0; if (ok) { for (var w : assigned.values()) totalSimplicity += w.simpel; @@ -628,10 +640,7 @@ public record SwedishGenerator(int W, int H, int SIZE, int MAX_LEN, int[] buff) static Undo placeWord(Grid grid, Slot s, Lemma w) { var ur = new UndoPlace[s.len]; - /* var urs = new int[s.len]; - var ucs = new int[s.len]; - var up = new char[s.len];*/ - var n = 0; + var n = 0; for (var i = 0; i < s.len; i++) { int r = s.r(i), c = s.c(i); @@ -639,9 +648,6 @@ public record SwedishGenerator(int W, int H, int SIZE, int MAX_LEN, int[] buff) var ch = w.charAt(i); if (cur == C_DASH) { ur[n] = new UndoPlace(r, c, C_DASH); - /*urs[n] = r; - ucs[n] = c; - up[n] = C_DASH;*/ n++; grid.setCharAt(r, c, ch); } else { @@ -662,13 +668,11 @@ public record SwedishGenerator(int W, int H, int SIZE, int MAX_LEN, int[] buff) public FillResult fillMask(Rng rng, Grid mask, DictEntry[] dictIndex, int logEveryMs, int timeLimitMs, boolean verbose) { - var grid = mask.deepCopyGrid(); - var allSlots = extractSlots(grid); - var slots = new ArrayList(); - for (var s : allSlots) if (s.len >= MIN_LEN && s.len <= MAX_LEN) slots.add(s); + var grid = mask.deepCopyGrid(); + var slots = extractSlots(grid); var used = new BitSet(); - var assigned = new HashMap(); + var assigned = new HashMap(); var cellCount = new int[H][W]; for (var s : slots) for (var i = 0; i < s.len; i++) cellCount[s.r(i)][s.c(i)]++; diff --git a/src/test/java/puzzle/SlotTest.java b/src/test/java/puzzle/SlotTest.java new file mode 100644 index 0000000..e61410f --- /dev/null +++ b/src/test/java/puzzle/SlotTest.java @@ -0,0 +1,18 @@ +package puzzle; + +import org.junit.jupiter.api.Test; +import puzzle.SwedishGenerator.Slot; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class SlotTest { + + @Test + public void testHoriz() { + assertTrue(new Slot(0, 0, 2, 0L, 0L, 5).horiz()); + assertTrue(new Slot(0, 0, 4, 0L, 0L, 5).horiz()); + assertFalse(new Slot(0, 0, 1, 0L, 3L, 5).horiz()); + assertFalse(new Slot(0, 0, 3, 0L, 3L, 5).horiz()); + assertFalse(new Slot(0, 0, 5, 0L, 3L, 5).horiz()); + } +} \ No newline at end of file