Skip to content

Add tests that solve crossword puzzles #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.org
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ It is based on [[https://github.com/chocoteam/choco-solver][choco-solver]] which
- [[http://www.cadaeic.net/alphas.htm][An Alphametic Page]], Mike Keith - a must for alphametic lovers.
- [[http://cryptarithms.awardspace.us/][Cryptarithms Online]], Jorge A. C. B. Soares - it contains the Sphinx collection, links, books.
- [[http://www.tkcs-collins.com/truman/alphamet/alphamet.shtml][Alphametic Puzzles]], Truman Collins - he has solvers and even generators.
- [[https://www.math.uni-bielefeld.de/~sillke/PUZZLES/ALPHAMETIC/][Alphametic]], Torsten Sillke - collection, variants, references, and links.
- [[https://www.math.uni-bielefeld.de/~sillke/PUZZLES/ALPHAMETIC/][Alphametics]], Torsten Sillke - collection, variants, references, and links.
- You can also browse our [[file:src/main/cryptarithms/][collection of cryptarithms]].

* Download and Installation
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/cryptator/BignumTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@

import cryptator.parser.CryptaParserException;
import cryptator.solver.CryptaModelException;
import cryptator.solver.CryptaSolver;
import cryptator.solver.CryptaSolverException;

public class BignumTest {

private final CryptaSolvingTester t = new CryptaSolvingTester(new CryptaSolver(true));
private final CryptaSolvingTester t = new CryptaSolvingTester(true);

public BignumTest() {
}
Expand Down
23 changes: 2 additions & 21 deletions src/test/java/cryptator/ExtensiveTesting.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,15 @@
*/
package cryptator;

import java.io.InputStream;
import java.util.Scanner;

import org.junit.BeforeClass;
import org.junit.Test;

import cryptator.parser.CryptaParserException;
import cryptator.solver.CryptaModelException;
import cryptator.solver.CryptaSolver;
import cryptator.solver.CryptaSolverException;

public class ExtensiveTesting {

private final CryptaSolvingTester t = new CryptaSolvingTester(new CryptaSolver(false));

public ExtensiveTesting() {
}

Expand All @@ -32,20 +26,7 @@ public static void configureTestLoggers() {
}

@Test
public void testAll() throws CryptaParserException, CryptaModelException, CryptaSolverException {
final InputStream in = getClass().getClassLoader().getResourceAsStream("cryptarithms-barker.txt");
final Scanner s = new Scanner(in);
try {
// s.skip("\\s*#.*"); // not working
while (s.hasNextLine()) {
final String line = s.nextLine();
if (!line.matches("\\s*#.*")) {
t.testUNIQUE(line);
}
}
} finally {
s.close();
}
public void testBarker() throws CryptaParserException, CryptaModelException, CryptaSolverException {
(new CryptaSolvingTester(false)).testResource("cryptarithms-barker.db.txt");
}

}
31 changes: 30 additions & 1 deletion src/test/java/cryptator/SolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.InputStream;
import java.math.BigInteger;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Before;
Expand All @@ -39,6 +41,10 @@ final class CryptaSolvingTester {
protected final ICryptaEvaluation eval = new CryptaEvaluation();
protected CryptaConfig config = new CryptaConfig();

CryptaSolvingTester(final boolean useBignum) {
this(new CryptaSolver(useBignum));
}

CryptaSolvingTester(final ICryptaSolver solver) {
super();
this.solver = solver;
Expand Down Expand Up @@ -87,11 +93,28 @@ public void testUNIQUE(final String cryptarithm) throws CryptaModelException, Cr
public void testNotUNIQUE(final String cryptarithm) throws CryptaModelException, CryptaSolverException {
assertTrue("solution count " + cryptarithm, testSolve(cryptarithm, true) > 1);
}

public void testResource(String resourcePath)
throws CryptaParserException, CryptaModelException, CryptaSolverException {
final InputStream in = getClass().getClassLoader().getResourceAsStream(resourcePath);
final Scanner s = new Scanner(in);
try {
// s.skip("\\s*#.*"); // not working
while (s.hasNextLine()) {
final String line = s.nextLine();
if (!line.matches("\\s*#.*")) {
testUNIQUE(line);
}
}
} finally {
s.close();
}
}
}

public class SolverTest {

private CryptaSolvingTester t = new CryptaSolvingTester(new CryptaSolver(false));
private CryptaSolvingTester t = new CryptaSolvingTester(false);

public SolverTest() {
}
Expand Down Expand Up @@ -735,4 +758,10 @@ public void testEvaluation4Issue25() throws CryptaParserException, CryptaSolverE
var cryptarithm = "W='4'";
t.testUNIQUE(cryptarithm);
}

@Test
public void testSillke() throws CryptaParserException, CryptaModelException, CryptaSolverException {
t.testResource("crosswords-sillke.db.txt");
}

}
31 changes: 31 additions & 0 deletions src/test/resources/crosswords-sillke.db.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
ABC*DA=BEFA && GHHA+GJE=GFDC && GJEJ+HJJJ=FFDC && ABC+GHHA=GJEJ && DA*GJE=HJJJ && BEFA-GFDC=FFDC
ABCD/EFG=HC && FHA-IG=FJD && AHBE-ACFE=FAJ && ABCD-FHA=AHBE && EFG*IG=ACFE && HC+FJD=FAJ
ABC*DE=CFGH && JDHJ+DGC=JGKK && JEDK+EBAH=FAGH && ABC+JDHJ=JEDK && DE*DGC=EBAH && CFGH-JGKK=FAGH
AB*CDC=BABE && AFD+GHJE=GGDC && HFKH+GJEF=KDAH && AB*AFD=HFKH && CDC+GHJE=GJEF && BABE-GGDC=KDAH
EGA*AH=DGFG && LL+EFFC=EFDL && ABCD+EFGH=KLBK && EGA*LL=ABCD && AH+EFFC=EFGH && DGFG-EFDL=KLBK
ABAC/DEF=GB && HEGF-HBID=HED && EKBI-HKGI=ICDC && ABAC-HEGF=EKBI && DEF+HBID=HKGI && GB*HED=ICDC
ABBC/DDE=DF && AGFG-AHIB=KKG && KGEH+ACKE=FCII && ABBC+AGFG=KGEH && DDE+AHIB=ACKE && DF*KKG=FCII
ABCD+DCEF=GHFI && AHGE+AIC=AKGF && KEG*KI=EBKB && ABCD-AHGE=KEG && DCEF/AIC=KI && GHFI+AKGF=EBKB
ABCD+EBFG=FDBH && AE+EBII=EBAE && AHC*FG=KFGK && ABCD/AE=AHC && EBFG-EBII=FG && FDBH+EBAE=KFGK
ABCD-EABF=GDGA && HII+EGKD=EFCK && IH*EDH=HABF && ABCD/HII=IH && EABF-EGKD=EDH && GDGA-EFCK=HABF
ABCD+EFCF=BGHE && AAIF+GDE=AEDC && FCH*GK=KHHI && ABCD-AAIF=FCH && EFCF/GDE=GK && BGHE+AEDC=KHHI
ABBC/DE=FGD && FBHI-FGKD=FD && EHGI-FBCG=CCEK && ABBC-FBHI=EHGI && DE+FGKD=FBCG && FGD*FD=CCEK
ABCD+EFGG=HEBG && CI+EHKI=EFCH && CKB*GH=IKFD && ABCD/CI=CKB && EFGG-EHKI=GH && HEBG+EFCH=IKFD
ABCD/EE=EFD && GHEH-GIKC=IF && HHDI+GICF=DBCA && ABCD+GHEH=HHDI && EE+GIKC=GICF && EFD*IF=DBCA
ABCD+EFGH=KLBK && LL+EFFC=EFDL && EGA*AH=DGFG && ABCD/LL=EGA && EFGH-EFFC=AH && KLBK+EFDL=DGFG
ABCDE-FGEE=AHBDE && DI*HDB=BKDB && CDE+FKCD=KDGD && ABCDE/DI=CDE && FGEE-HDB=FKCD && AHBDE-BKDB=KDGD
ATU+IAS=IITE && NEG/IOG=E && PAU-NS=PPA && ATU-NEG=PAU && IAS-IOG=NS && IITE/E=PPA
HEB+EUS=LFS && WUB/EH=US && EHB-ESU=RA && HEB-WUB=EHB && EUS+EH=ESU && LFS/US=RA
ABC*DC=EAEF && BF+AEGG=AEBF && FFEE+AEDC=DBHG && ABC*BF=FFEE && DC+AEGG=AEDC && EAEF-AEBF=DBHG
ABC-DBB=EFG && ABB-ECC=BBB && DBEB-HBB=IBC && ABC+ABB=DBEB && DBB+ECC=HBB && EFG+BBB=IBC
AB*BC=DAD && EC/F=F && BCD*G=BBEA && AB*EC=BCD && BC/F=G && DAD*F=BBEA
HJ+AD=DF && BG+EC=DF && AC+GE=HEK && HJ+BG=AC && AD+EC=GE && DF+DF=HEK
ISE+EID=ALK && RAS-KL=RUM && ERR+MDR=LLM && ISE-RAS=ERR && EID+KL=MDR && ALK-RUM=LLM
ABC-DEF=EG && HIE/DAH=H && FBD/HB=DF && ABC+HIE=FBD && DEF-DAH=HB && EG/H=DF
AB*A=CCD && AE/F=G && B*CF=CHE && AB-AE=B && A*F=CF && CCD+G=CHE
E*K=BE && B/N=N && DN+DN=NB && E+B=DN && K*N=DN && BE/N=NB
AB*C=CD && E/B=F && F+G=H && AB-E=F && C+B=G && CD/F=H
AB*C=DEB && AC*B=FBB && GE-FE=CE && AB+AC=GE && C+B=FE && DEB-FBB=CE
AB*B=CDD && CB/B=E && E*D=BD && AB/CB=E && B*B=D && CDD/E=BD
AB*C=DE && F*F=AC && GH+AI=CH && AB*F=GH && C+F=AI && DE-AC=CH
ABC/CD=EF && FDF+EG=AHD && BB+FGA=AIJ && ABC-FDF=BB && CD*EG=FGA && EF+AHD=AIJ