Skip to content

Instantly share code, notes, and snippets.

@tcolgate
Last active August 26, 2020 10:12
Show Gist options
  • Save tcolgate/05d0d4b2628bd8f30eac90fccdd0a64c to your computer and use it in GitHub Desktop.
Save tcolgate/05d0d4b2628bd8f30eac90fccdd0a64c to your computer and use it in GitHub Desktop.
find all valid pins for silly rules
digit(0).
digit(1).
digit(2).
digit(3).
digit(4).
digit(5).
digit(6).
digit(7).
digit(8).
digit(9).
isDigits([]).
isDigits([H|T]) :-
digit(H),
isDigits(T).
consecutive([A|[A|_]]).
consecutive([_|T]) :- consecutive(T).
increasing([A,B]) :- B > A.
increasing([A|[B|T]]) :-
B > A,
increasing([B|T]).
decreasing([A,B]) :- B < A.
decreasing([A|[B|T]]) :-
B < A,
decreasing([B|T]).
allSame([A,A]).
allSame([A|[A|T]]) :- allSame([A|T]).
numDiff(A,B,C) :- C is abs(B - A).
diff([],[]).
diff([_],[]).
diff([A,B],L) :-
numDiff(A,B,D),
L = [D].
diff([A|[B|T]],L) :-
diff([B|T],TD),
numDiff(A,B,D),
L = [D|TD].
linear(L) :- decreasing(L),!.
linear(L) :- increasing(L).
invalid([H|T]) :- consecutive([H|T]).
invalid([H|T]) :- linear([H|T]).
invalid([H|T]) :-
diff([H|T], Diffs),
allSame(Diffs).
pin(L,[H|T]) :-
length([H|T],L),
digit(H),
H > 1,
isDigits([H|T]),
not(invalid([H|T])).
countValid(L,Count) :-
aggregate_all(count,pin(L,P),Count).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment