/** * MIT License * * Copyright (c) 2021 Exercism * Copyright (c) 2026 Networked Embedded Systems, University of Duisburg-Essen * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import java.util.Map; import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.annotation.ElementType; public class AlphameticsMain { @FunctionalInterface private interface ThrowingRunnable { void run() throws UnsolvablePuzzleException; } @Test(name = "puzzle with three letters") public void testThreeLetters() { runTest(() -> { Map result = new Alphametics("I + BB == ILL").solve(); assertMapEquals(Map.ofEntries(Map.entry('I', 1), Map.entry('B', 9), Map.entry('L', 0)), result); }); } @Test(name = "solution must have unique value for each letter") @Disabled public void testUniqueValue() { runTest(() -> { Alphametics alphametics = new Alphametics("A == B"); assertThrows(UnsolvablePuzzleException.class, alphametics::solve, null); }); } @Test(name = "leading zero solution is invalid") @Disabled public void testLeadingZero() { runTest(() -> { Alphametics alphametics = new Alphametics("ACA + DD == BD"); assertThrows(UnsolvablePuzzleException.class, alphametics::solve, null); }); } @Test(name = "puzzle with two digits final carry") @Disabled public void testTwoDigitsFinalCarry() { runTest(() -> { Map result = new Alphametics("A + A + A + A + A + A + A + A + A + A + A + B == BCC") .solve(); assertMapEquals(Map.ofEntries(Map.entry('A', 9), Map.entry('B', 1), Map.entry('C', 0)), result); }); } @Test(name = "puzzle with four letters") @Disabled public void testFourLetters() { runTest(() -> { Map result = new Alphametics("AS + A == MOM").solve(); assertMapEquals(Map.ofEntries(Map.entry('A', 9), Map.entry('S', 2), Map.entry('M', 1), Map.entry('O', 0)), result); }); } @Test(name = "puzzle with six letters") @Disabled public void testSixLetters() { runTest(() -> { Map result = new Alphametics("NO + NO + TOO == LATE").solve(); assertMapEquals(Map.ofEntries(Map.entry('N', 7), Map.entry('O', 4), Map.entry('T', 9), Map.entry('L', 1), Map.entry('A', 0), Map.entry('E', 2)), result); }); } @Test(name = "puzzle with seven letters") @Disabled public void testSevenLetters() { runTest(() -> { Map result = new Alphametics("HE + SEES + THE == LIGHT").solve(); assertMapEquals(Map.ofEntries(Map.entry('E', 4), Map.entry('G', 2), Map.entry('H', 5), Map.entry('I', 0), Map.entry('L', 1), Map.entry('S', 9), Map.entry('T', 7)), result); }); } @Test(name = "puzzle with eight letters") @Disabled public void testEightLetters() { runTest(() -> { Map result = new Alphametics("SEND + MORE == MONEY").solve(); assertMapEquals(Map.ofEntries(Map.entry('S', 9), Map.entry('E', 5), Map.entry('N', 6), Map.entry('D', 7), Map.entry('M', 1), Map.entry('O', 0), Map.entry('R', 8), Map.entry('Y', 2)), result); }); } @Test(name = "puzzle with ten letters") @Disabled public void testTenLetters() { runTest(() -> { Map result = new Alphametics("AND + A + STRONG + OFFENSE + AS + A + GOOD == DEFENSE") .solve(); assertMapEquals(Map.ofEntries(Map.entry('A', 5), Map.entry('D', 3), Map.entry('E', 4), Map.entry('F', 7), Map.entry('G', 8), Map.entry('N', 0), Map.entry('O', 2), Map.entry('R', 1), Map.entry('S', 6), Map.entry('T', 9)), result); }); } @Test(name = "puzzle with ten letters and 199 addends") @Disabled public void testTenLetters199Addends() { runTest(() -> { Map result = new Alphametics( "THIS + A + FIRE + THEREFORE + FOR + ALL + HISTORIES + I + TELL + A + " + "TALE + THAT + FALSIFIES + ITS + TITLE + TIS + A + LIE + THE + TALE + OF + THE + LAST + FIRE + " + "HORSES + LATE + AFTER + THE + FIRST + FATHERS + FORESEE + THE + HORRORS + THE + LAST + FREE + " + "TROLL + TERRIFIES + THE + HORSES + OF + FIRE + THE + TROLL + RESTS + AT + THE + HOLE + OF + " + "LOSSES + IT + IS + THERE + THAT + SHE + STORES + ROLES + OF + LEATHERS + AFTER + SHE + SATISFIES + " + "HER + HATE + OFF + THOSE + FEARS + A + TASTE + RISES + AS + SHE + HEARS + THE + LEAST + FAR + " + "HORSE + THOSE + FAST + HORSES + THAT + FIRST + HEAR + THE + TROLL + FLEE + OFF + TO + THE + " + "FOREST + THE + HORSES + THAT + ALERTS + RAISE + THE + STARES + OF + THE + OTHERS + AS + THE + " + "TROLL + ASSAILS + AT + THE + TOTAL + SHIFT + HER + TEETH + TEAR + HOOF + OFF + TORSO + AS + THE + " + "LAST + HORSE + FORFEITS + ITS + LIFE + THE + FIRST + FATHERS + HEAR + OF + THE + HORRORS + THEIR + " + "FEARS + THAT + THE + FIRES + FOR + THEIR + FEASTS + ARREST + AS + THE + FIRST + FATHERS + " + "RESETTLE + THE + LAST + OF + THE + FIRE + HORSES + THE + LAST + TROLL + HARASSES + THE + FOREST + " + "HEART + FREE + AT + LAST + OF + THE + LAST + TROLL + ALL + OFFER + THEIR + FIRE + HEAT + TO + THE + " + "ASSISTERS + FAR + OFF + THE + TROLL + FASTS + ITS + LIFE + SHORTER + AS + STARS + RISE + THE + " + "HORSES + REST + SAFE + AFTER + ALL + SHARE + HOT + FISH + AS + THEIR + AFFILIATES + TAILOR + A + " + "ROOFS + FOR + THEIR + SAFE == FORTRESSES") .solve(); assertMapEquals(Map.ofEntries(Map.entry('A', 1), Map.entry('E', 0), Map.entry('F', 5), Map.entry('H', 8), Map.entry('I', 7), Map.entry('L', 2), Map.entry('O', 6), Map.entry('R', 3), Map.entry('S', 4), Map.entry('T', 9)), result); }); } private void assertMapEquals(Map expected, Map actual) { if (expected == null ? actual == null : expected.equals(actual)) { return; } throw new AssertionError("Expected: " + expected + ", but got: " + actual); } private void assertThrows(Class expectedType, ThrowingRunnable runnable, String expectedMessage) { try { runnable.run(); throw new AssertionError("Expected exception: " + expectedType.getSimpleName()); } catch (UnsolvablePuzzleException t) { if (expectedType.isInstance(t) && (expectedMessage == null || expectedMessage.equals(t.getMessage()))) { return; } throw new AssertionError("Expected: " + expectedType.getSimpleName() + " with message: " + expectedMessage + ", but got: " + t.getClass().getSimpleName() + " with message: " + t.getMessage()); } } private void runTest(Runnable body) { try { body.run(); System.out.println("TEST PASSED"); } catch (Exception | AssertionError t) { System.out.println("TEST FAILED: " + t.getMessage()); } } public static void main(String[] args) { new AlphameticsMain().runTests(); } /** * Annotation to mark test methods. The name parameter is used to identify the * test in the output. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) private @interface Test { String name(); } /** * Annotation to mark test methods as disabled. Disabled tests will not be * executed when running the tests. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) private @interface Disabled { } private void runTests() { Method[] methods = this.getClass().getDeclaredMethods(); List allTestMethods = Arrays.asList(methods).stream().filter(m -> m.isAnnotationPresent(Test.class)) .collect(Collectors.toList()); List enabledTestMethods = allTestMethods.stream().filter(m -> !m.isAnnotationPresent(Disabled.class)) .collect(Collectors.toList()); System.out .println("Running " + enabledTestMethods.size() + " out of " + allTestMethods.size() + " tests... \n"); for (Method method : enabledTestMethods) { try { System.out.println("Running test: " + method.getAnnotation(Test.class).name()); method.invoke(this); } catch (Exception e) { System.out.println("Test " + method.getAnnotation(Test.class).name() + " failed with an unexpected exception: " + e.getCause()); } } System.out.println("Finished running tests."); if (allTestMethods.size() != enabledTestMethods.size()) { System.out.println("\nThe following tests are disabled and were not executed:"); for (Method method : allTestMethods) { if (method.isAnnotationPresent(Disabled.class)) { System.out.println("- " + method.getAnnotation(Test.class).name()); } } System.out.println( "To enable these tests, remove the @Disabled annotation from the corresponding test methods."); } } }