Created
October 31, 2024 20:36
-
-
Save derrickturk/6ab847b0020c4023a74296d0aa03fc41 to your computer and use it in GitHub Desktop.
DOG + ANT = CAT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:- module dogcrypt2. | |
:- interface. | |
:- import_module io. | |
:- pred main(io::di, io::uo) is det. | |
:- implementation. | |
:- import_module int, list, string, solutions. | |
main(!IO) :- | |
io.format("DOG + ANT = CAT\n", [], !IO), | |
Solns = solutions(equation), | |
( if Solns = [] | |
then | |
io.format("no solutions!\n", [], !IO) | |
else | |
foldl((pred({DOG, ANT, CAT}::in, !.IO::di, !:IO::uo) is det :- | |
io.format("%d + %d = %d\n", [i(DOG), i(ANT), i(CAT)], !IO)), | |
Solns, | |
!IO) | |
). | |
:- pred equation({int, int, int}::out) is nondet. | |
equation({DOG, ANT, CAT}) :- | |
Ds0 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], | |
pick(Ds0, D, Ds1), | |
pick(Ds1, O, Ds2), | |
pick(Ds2, G, Ds3), | |
pick(Ds3, A, Ds4), | |
pick(Ds4, N, Ds5), | |
pick(Ds5, T, Ds6), | |
pick(Ds6, C, _), | |
D \= 0, | |
A \= 0, | |
C \= 0, | |
DOG = 100 * D + 10 * O + G, | |
ANT = 100 * A + 10 * N + T, | |
CAT = 100 * C + 10 * A + T, | |
DOG + ANT = CAT. | |
:- pred pick(list(int)::in, int::out, list(int)::out) is nondet. | |
pick([X | Xs], X, Xs). | |
pick([X | Xs], Y, [X | Zs]) :- pick(Xs, Y, Zs). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment