You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

:- discontiguous task_info/0.
vessels([бутылка, стакан, кувшин, банка]).
liquids([молоко, лимонад, квас, вода]).
index_of([H|_], H, 1) :- !.
index_of([_|T], X, I) :- index_of(T, X, I1), I is I1 + 1.
adjacent(I, J) :- J is I + 1 ; I is J + 1.
between_pos(Mid, Left, Right) :-
(Mid > Left, Mid < Right) ; (Mid < Left, Mid > Right).
solve_puzzle(Solution) :-
vessels(VNames),
liquids(Ls),
% Перестановка судов и жидкостей по позициям 1..4
permutation(VNames, [V1, V2, V3, V4]),
permutation(Ls, [L1, L2, L3, L4]),
Solution = [(V1,L1),(V2,L2),(V3,L3),(V4,L4)],
Vessels = [V1,V2,V3,V4],
Liquids = [L1,L2,L3,L4],
% Условие 1: Вода и молоко не в бутылке
( V1 = бутылка -> (L1 \= вода, L1 \= молоко) ; true),
( V2 = бутылка -> (L2 \= вода, L2 \= молоко) ; true),
( V3 = бутылка -> (L3 \= вода, L3 \= молоко) ; true),
( V4 = бутылка -> (L4 \= вода, L4 \= молоко) ; true),
% Условие 2: Сосуд с лимонадом находится между кувшином и сосудом с квасом
index_of(Vessels, кувшин, JugPos),
index_of(Liquids, лимонад, LemonPos),
( (L1 = квас, KvassPos = 1)
; (L2 = квас, KvassPos = 2)
; (L3 = квас, KvassPos = 3)
; (L4 = квас, KvassPos = 4)
),
between_pos(LemonPos, JugPos, KvassPos),
% Условие 3: В банке — не лимонад и не вода
( V1 = банка -> (L1 \= лимонад, L1 \= вода) ; true),
( V2 = банка -> (L2 \= лимонад, L2 \= вода) ; true),
( V3 = банка -> (L3 \= лимонад, L3 \= вода) ; true),
( V4 = банка -> (L4 \= лимонад, L4 \= вода) ; true),
% Условие 4: Стакан находится около банки и сосуда с молоком
index_of(Vessels, стакан, GlassPos),
index_of(Vessels, банка, JarPos),
( (L1 = молоко, MilkPos = 1)
; (L2 = молоко, MilkPos = 2)
; (L3 = молоко, MilkPos = 3)
; (L4 = молоко, MilkPos = 4)
),
adjacent(GlassPos, JarPos),
adjacent(GlassPos, MilkPos).
pretty_print_solution(Solution) :-
write('Решение (слева направо):'), nl,
forall(member((V,L), Solution), (write(' '), write(V), write(' — '), write(L), nl)).
solve_and_print :-
findall(S, solve_puzzle(S), Sols),
forall(member(S, Sols), (pretty_print_solution(S), nl)).
task_info :-
write('Логическая задача о четырех сосудах и четырех жидкостях.'), nl,
write('Вызовите solve_and_print. Или solve_puzzle(Solution).'), nl.
show_examples :-
write('Примеры запросов:'), nl,
write('?- solve_puzzle(S), pretty_print_solution(S).'), nl,
write('?- solve_and_print.'), nl.