|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import org.chocosolver.solver.Model |
| 17 | +import org.chocosolver.solver.variables.IntVar |
| 18 | + |
| 19 | +new Model("ABCD*4=DCBA").with { |
| 20 | + def (A, D) = ['A', 'D'].collect { intVar(it, 1, 9) } |
| 21 | + def (B, C) = ['B', 'C'].collect { intVar(it, 0, 9) } |
| 22 | + def R = (0..2).collect { intVar(0, 9) } |
| 23 | + |
| 24 | + allDifferent(A, B, C, D).post() |
| 25 | + R[2].add(A.mul(4)).eq(D).post() |
| 26 | + R[1].add(B.mul(4)).eq(C.add(R[2].mul(10))).post() |
| 27 | + R[0].add(C.mul(4)).eq(B.add(R[1].mul(10))).post() |
| 28 | + D.mul(4).eq(A.add(R[0].mul(10))).post() |
| 29 | + solver.findAllSolutions().each { |
| 30 | + println "$name: ${pretty(it, [A, B, C, D, ' * 4 = ', D, C, B, A])}\n$it\n" |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +new Model("AA+BB+CC=ABC").with { |
| 35 | + def (A, B, C) = ['A', 'B', 'C'].collect { intVar(it, 1, 9) } |
| 36 | + allDifferent(A, B, C).post() |
| 37 | + A.mul(11).add(B.mul(11).add(C.mul(11))).eq(A.mul(100).add(B.mul(10)).add(C)).post() |
| 38 | + solver.findAllSolutions().each { |
| 39 | + println "$name: ${pretty(it, [A, A, ' + ', B, B, ' + ', C, C, ' = ', A, B, C])}\n$it\n" |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +new Model("HALF+HALF=WHOLE").with { |
| 44 | + def (H, W) = ['H', 'W'].collect { intVar(it, 1, 9) } |
| 45 | + def (A, E, F, L, O) = ['A', 'E', 'F', 'L', 'O'].collect { intVar(it, 0, 9) } |
| 46 | + allDifferent(H, W, A, E, F, L, O).post() |
| 47 | + IntVar[] ALL = [ |
| 48 | + H, A, L, F, |
| 49 | + W, H, O, L, E] |
| 50 | + int[] COEFFS = [ |
| 51 | + 2000, 200, 20, 2, |
| 52 | + -10000, -1000, -100, -10, -1] |
| 53 | + scalar(ALL, COEFFS, "=", 0).post() |
| 54 | + solver.findAllSolutions().each { |
| 55 | + println "$name: ${pretty(it, [H, A, L, F, ' + ', H, A, L, F, ' = ', W, H, O, L, E])}\n$it\n" |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +new Model("HALF+FIFTH+TENTH+TENTH+TENTH=WHOLE").with { |
| 60 | + def (H, F, T, W) = ['H', 'F', 'T', 'W'].collect { intVar(it, 1, 9) } |
| 61 | + def (A, L, I, E, N, O) = ['A', 'L', 'I', 'E', 'N', 'O'].collect { intVar(it, 0, 9) } |
| 62 | + allDifferent(H, F, T, W, A, L, I, E, N, O).post() |
| 63 | + IntVar[] ALL = [ |
| 64 | + H, A, L, F, |
| 65 | + F, I, F, T, H, |
| 66 | + T, E, N, T, H, |
| 67 | + T, E, N, T, H, |
| 68 | + T, E, N, T, H, |
| 69 | + W, H, O, L, E] |
| 70 | + int[] COEFFS = [ |
| 71 | + 1000, 100, 10, 1, |
| 72 | + 10000, 1000, 100, 10, 1, |
| 73 | + 10000, 1000, 100, 10, 1, |
| 74 | + 10000, 1000, 100, 10, 1, |
| 75 | + 10000, 1000, 100, 10, 1, |
| 76 | + -10000, -1000, -100, -10, -1] |
| 77 | + scalar(ALL, COEFFS, "=", 0).post() |
| 78 | + solver.findAllSolutions().each { |
| 79 | + def parts = [H, A, L, F, '+', F, I, F, T, H, '+', T, E, N, T, H, '+', |
| 80 | + T, E, N, T, H, '+', T, E, N, T, H, '=', W, H, O, L, E] |
| 81 | + println "$name: ${pretty(it, parts)}\n$it\n" |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// helper method to print solutions |
| 86 | +def pretty(model, parts) { |
| 87 | + parts.collect { p -> p instanceof IntVar ? model.getIntVal(p) : p }.join() |
| 88 | +} |
0 commit comments