From 8e7b29a2d38364846d361a7395a77fb5e6072eb7 Mon Sep 17 00:00:00 2001 From: mike Date: Mon, 12 Jan 2026 21:29:06 +0100 Subject: [PATCH] introduce bitloops --- src/main/java/puzzle/CsvIndexService.java | 2 ++ src/main/java/puzzle/Export.java | 14 ++++++-------- src/main/java/puzzle/HintScores.java | 1 + src/main/java/puzzle/Main.java | 14 ++++++++++++++ src/main/java/puzzle/SwedishGenerator.java | 2 +- src/test/java/puzzle/SwedishGeneratorTest.java | 6 +++--- 6 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/main/java/puzzle/CsvIndexService.java b/src/main/java/puzzle/CsvIndexService.java index 97c13a2..ccbcca1 100644 --- a/src/main/java/puzzle/CsvIndexService.java +++ b/src/main/java/puzzle/CsvIndexService.java @@ -68,6 +68,7 @@ public final class CsvIndexService return lineToSimpel(SC.get().getLine(index)); return -1; } catch (Exception e) { + e.printStackTrace(); throw new RuntimeException("Failed to get clues for index " + index, e); } } @@ -77,6 +78,7 @@ public final class CsvIndexService return lineToClue(SC.get().getLine(index)); return new String[0]; } catch (Exception e) { + e.printStackTrace(); throw new RuntimeException("Failed to get clues for index " + index, e); } } diff --git a/src/main/java/puzzle/Export.java b/src/main/java/puzzle/Export.java index d0b57fc..a362729 100644 --- a/src/main/java/puzzle/Export.java +++ b/src/main/java/puzzle/Export.java @@ -159,15 +159,17 @@ public record Export() { public void clear() { Arrays.fill(bits, 0L); } } - record Placed(long lemma, char direction, int slotKey, int[] cells) { + record Placed(long lemma, int slotKey, int[] cells) { - public static final char HORIZONTAL = 'h'; - static final char VERTICAL = 'v'; + static final char[] DIRECTION = { '\0', Placed.VERTICAL, Placed.HORIZONTAL, Placed.VERTICAL, Placed.HORIZONTAL, Placed.VERTICAL }; + public static final char HORIZONTAL = 'h'; + static final char VERTICAL = 'v'; public int arrowCol() { return Grid.c(Slot.clueIndex(slotKey)); } public int arrowRow() { return Grid.r(Slot.clueIndex(slotKey)); } public int startRow() { return Grid.r(cells[0]); } public int startCol() { return Grid.c(cells[0]); } public boolean isReversed() { return !Slot.increasing(slotKey); } + public char direction() { return DIRECTION[Slot.dir(slotKey)]; } } public record Rewards(int coins, int stars, int hints) { } @@ -185,15 +187,11 @@ public record Export() { boolean inBounds(int idx) { return idx >= 0 && idx < SwedishGenerator.SIZE; } Placed extractPlacedFromSlot(Slot s, long lemma) { - var d = s.dir(); var cells = s.walk().toArray(); - char[] DIRECTION = { '\0', Placed.VERTICAL, Placed.HORIZONTAL, Placed.VERTICAL, Placed.HORIZONTAL, Placed.VERTICAL }; - return new Placed( lemma, - DIRECTION[d], s.key(), cells ); @@ -258,7 +256,7 @@ public record Export() { p.lemma, p.startRow() - MIN_R, p.startCol() - MIN_C, - p.direction, + p.direction(), p.arrowRow() - MIN_R, p.arrowCol() - MIN_C, p.isReversed() diff --git a/src/main/java/puzzle/HintScores.java b/src/main/java/puzzle/HintScores.java index feab4dd..a20b199 100644 --- a/src/main/java/puzzle/HintScores.java +++ b/src/main/java/puzzle/HintScores.java @@ -82,6 +82,7 @@ public final class HintScores { try { score = scoreFn.applyAsInt(word); } catch (RuntimeException ex) { + ex.printStackTrace(); // If scoring fails, decide your policy: skip or set 0. // Here: skip row. continue; diff --git a/src/main/java/puzzle/Main.java b/src/main/java/puzzle/Main.java index bf144d1..831a585 100644 --- a/src/main/java/puzzle/Main.java +++ b/src/main/java/puzzle/Main.java @@ -125,6 +125,7 @@ public class Main { else rebuildIndex(); info("indexUpdated : " + INDEX_FILE); } catch (IOException e) { + e.printStackTrace(); err("Failed to write: " + FILE_NAME); err("Reason : " + e.getMessage()); System.exit(2); @@ -344,6 +345,15 @@ public class Main { } static PuzzleResult attempt(Rng rng, Dict dict, Opts opts) { + try { + return _attempt(rng, dict, opts); + } catch (Exception e) { + e.printStackTrace(); + System.err.println("Failed to operate" + e.getMessage()); + return null; + } + } + static PuzzleResult _attempt(Rng rng, Dict dict, Opts opts) { TOTAL_ATTEMPTS.incrementAndGet(); var swe = new SwedishGenerator(rng); var mask = swe.generateMask(opts.pop, opts.gens, Math.max(opts.pop, (int) Math.floor(opts.pop * 1.5))); @@ -422,6 +432,7 @@ public class Main { Files.writeString(indexPath, content, StandardCharsets.UTF_8); info("indexUpdated : " + indexPath); } catch (IOException e) { + e.printStackTrace(); err("Failed to update index.json: " + e.getMessage()); } } @@ -462,10 +473,12 @@ public class Main { var pathInIndex = "/puzzles/" + filename; records.add(toIndexRecordJson(id, pathInIndex, date, theme, difficulty, createdAt)); } catch (IOException e) { + e.printStackTrace(); err("Failed to read " + path + ": " + e.getMessage()); } }); } catch (IOException e) { + e.printStackTrace(); err("Failed to list puzzles: " + e.getMessage()); return; } @@ -476,6 +489,7 @@ public class Main { Files.writeString(indexPath, content, StandardCharsets.UTF_8); info("Successfully rebuilt index.json with " + records.size() + " records."); } catch (IOException e) { + e.printStackTrace(); err("Failed to write index.json: " + e.getMessage()); } } diff --git a/src/main/java/puzzle/SwedishGenerator.java b/src/main/java/puzzle/SwedishGenerator.java index 0e6acb8..31a02ff 100644 --- a/src/main/java/puzzle/SwedishGenerator.java +++ b/src/main/java/puzzle/SwedishGenerator.java @@ -318,7 +318,7 @@ public record SwedishGenerator(Rng rng) { public int clueIndex() { return clueIndex(key); } public static int clueIndex(int key) { return key >>> BIT_FOR_DIR; } public int clueC() { return Grid.c((key >>> BIT_FOR_DIR)); } - public int dir() { return key & 7; } + public static int dir(int key) { return key & 7; } public boolean horiz() { return horiz(key); } public boolean reversed() { return (key & 2) == 0; } public boolean increasing() { return (key & 2) != 0; } diff --git a/src/test/java/puzzle/SwedishGeneratorTest.java b/src/test/java/puzzle/SwedishGeneratorTest.java index 123c689..ff78b51 100644 --- a/src/test/java/puzzle/SwedishGeneratorTest.java +++ b/src/test/java/puzzle/SwedishGeneratorTest.java @@ -147,10 +147,10 @@ public class SwedishGeneratorTest { lo |= 1L << Grid.offset(4, 5); var s = Slot.from(key, lo, 0L); - System.out.println("[DEBUG_LOG] s.dir() = " + s.dir()); + System.out.println("[DEBUG_LOG] s.dir() = " + Slot.dir(s.key())); assertEquals(2, s.clueR()); assertEquals(3, s.clueC()); - assertEquals(3, s.dir()); + assertEquals(3, Slot.dir(s.key())); assertFalse(s.horiz()); var cells = s.walk().toArray(); assertEquals(2, Grid.r(cells[0])); @@ -259,7 +259,7 @@ public class SwedishGeneratorTest { assertTrue(s.len() >= 2); assertEquals(0, s.clueR()); assertEquals(0, s.clueC()); - assertEquals(2, s.dir()); + assertEquals(2, Slot.dir(s.key())); } @Test