:- prompt(_, '').
:- use_module(library(readutil)).
goal_check([]):-!.
goal_check([[[],_]|Xs]):-goal_check(Xs).
goal_check([[[X,X,X,X],_]|Xs]):-goal_check(Xs).
move_check([X,X,X,Y],[X,X,X],[Y]):-X\=Y.
move_check([X,X,Y,Y2],[X,X],[Y,Y2]):-X\=Y.
move_check([X,Y,Y2,Y3],[X],[Y,Y2,Y3]):-X\=Y.
move_check([X,X,X],[X,X,X],[]).
move_check([X,X,Y],[X,X],[Y]):-X\=Y.
move_check([X,Y,Y2],[X],[Y,Y2]):-X\=Y.
move_check([X,X],[X,X],[]).
move_check([X,Y],[X],[Y]):-X\=Y.
move_check([X],[X],[]).
move_ok(_,[]).
move_ok([X|_],[X|_]).
head([Xs,_],Xs).
get2([_,Xs],Xs).
format_ans(Data):-member([_,E1],Data),maplist(swap,E1,E2),msort(E2,E3),
maplist
(get2
,E3
,E4
),maplist
(writeln
,E4
),nl,false
.format_ans(_).
my_equal(E1,E2):-maplist(head,E1,E1A),
maplist(head,E2,E1A).
my_equal_or_more(E1,E2):-maplist(head,E1,E1A),
maplist(head,E2,E2A),
msort([E1A,E2A],[E1A,E2A]).
move(Xs1,Res):-select(X1,Xs1,Xs2),
[E1,No1]=X1,
move_check(E1,E1Move,E1Next),
select(X2,Xs2,Xs),
[E2,No2]=X2,
move_ok(E1Move,E2),
append(E1Move,E2,E2Next),
length(E2Next,Len),
Len<5,
msort([[E1Next,No1],[E2Next,No2]|Xs],Res).
search_all_move(Data1,[Now1,Next]):-member([_,Now1],Data1),move(Now1,Next).
swap([Old1,Now1],[Now1,Old1]).
all_swap(Data,Res):-member(E1,Data),swap(E1,Res).
deduplication([],Data,Data):-!.
deduplication(_,[],[]):-!.
deduplication([[Old1,_]|Data1],[[_,Next1]|Data2],Res):-my_equal(Old1,Next1),!,deduplication(Data1,Data2,Res).
deduplication([[Old1,_]|Data1],[[Now2,Next2]|Data2],Res):-my_equal_or_more(Old1,Next2),!,
deduplication(Data1,[[Now2,Next2]|Data2],Res).
deduplication([E1|Data1],[E2|Data2],[E2|Res]):-!,
deduplication([E1|Data1],Data2,Res).
bfs(Data,[[Old1,Ans2]]):-member([Old1|Ans],Data),maplist(goal_check,Ans),!,[Ans2]=Ans.
bfs(Data1,[[Old1,Now1]|Res1]):-
setof(E1
,search_all_move
(Data1
,E1
),Data2
), sort(Data2,Data3),
maplist(swap,Data1,Data1Swap),
sort(Data1Swap,Data1Swap2),
deduplication(Data1Swap2,Data3,Data4),
deduplication(Data1,Data4,Data5),
sort(Data5,Data6),
bfs(Data6,Res1),
[[Now1,_]|_]=Res1,
member([Old1,Now1],Data1),
!.
main:-
process,
process:-
/*
ウォーターソートパズルを解くコード、答えの出力は手抜き状態、堀江 伸一
ただいまビーカーを並べ替えてない答えを出すための記述方法を思考中
バグがあったのでそれの修整を検討中
取り合えず完成かな?
バグが残ってないか作問して確かめたいな。
他人の作問をテストデータに使うのはこのサイトではできないな。
*/
msort([[[1,2,2,2],1],[[2,1,1,1],2],[[],3],[[],4]],Test),
bfs([[[],Test]],Ans),
format_ans(Ans),
:- main.
Oi0gc2V0X3Byb2xvZ19mbGFnKHZlcmJvc2Usc2lsZW50KS4KOi0gcHJvbXB0KF8sICcnKS4KOi0gdXNlX21vZHVsZShsaWJyYXJ5KHJlYWR1dGlsKSkuCgpnb2FsX2NoZWNrKFtdKTotIS4KZ29hbF9jaGVjayhbW1tdLF9dfFhzXSk6LWdvYWxfY2hlY2soWHMpLgpnb2FsX2NoZWNrKFtbW1gsWCxYLFhdLF9dfFhzXSk6LWdvYWxfY2hlY2soWHMpLgoKbW92ZV9jaGVjayhbWCxYLFgsWV0sW1gsWCxYXSxbWV0pOi1YXD1ZLgptb3ZlX2NoZWNrKFtYLFgsWSxZMl0sW1gsWF0sW1ksWTJdKTotWFw9WS4KbW92ZV9jaGVjayhbWCxZLFkyLFkzXSxbWF0sW1ksWTIsWTNdKTotWFw9WS4KbW92ZV9jaGVjayhbWCxYLFhdLFtYLFgsWF0sW10pLgptb3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