Skip to content

Instantly share code, notes, and snippets.

@NickGeek
Created August 31, 2021 04:41
Show Gist options
  • Save NickGeek/2e45dfc3f84d3c1c110ae5ba1a8cd46a to your computer and use it in GitHub Desktop.
Save NickGeek/2e45dfc3f84d3c1c110ae5ba1a8cd46a to your computer and use it in GitHub Desktop.
state([], [], _).
%% Basically these facts are our valid moves because we can always move to an empty state.
move(state([H|T], [], S3), state(T, [H], S3)).
move(state([H|T], S2, []), state(T, S2, [H])).
move(state(S1, [H|T], []), state(S1, T, [H])).
move(state([], [H|T], S3), state([H], T, S3)).
move(state([], S2, [H|T]), state([H], S2, T)).
move(state(S1, [], [H|T]), state(S1, [H], T)).
%% These are our valid moves because we can always move to a tower where we're smaller than the current head.
move(state([H|T], [H2|T2], S3), state(T, [H, H2|T2], S3)) :- H =< H2.
move(state([H|T], S2, [H2|T2]), state(T, S2, [H, H2|T2])) :- H =< H2.
move(state([H2|T2], [H|T], S3), state([H, H2|T2], T, S3)) :- H =< H2.
move(state(S1, [H|T], [H2|T2]), state(S1, T, [H, H2|T2])) :- H =< H2.
move(state([H2|T2], S2, [H|T]), state([H, H2|T2], S2, T)) :- H =< H2.
move(state(S1, [H2|T2], [H|T]), state(S1, [H, H2|T2], T)) :- H =< H2.
run(state([], [], _), _) :- !.
run(State, Visited) :-
not(member(State, Visited)), % Don't get stuck in a cycle
append(Visited, [State], NewVisited),
move(State, NewState),
run(NewState, NewVisited),
write('Moving from '), write(State), write(' to '), write(NewState), nl,
!.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment