% ppm.pi (in Picat) % https://www.mat.unical.it/aspcomp2013/FinalProblemDescriptions/PermutationPatternMatching % by Roman Bartak, Feb. 2013 % translate to Picat by Neng-Fa Zhou import cp. main => read_file_terms().asp(). go => test. asp(Facts), cl_facts(Facts), findall([I,P], t(I,P)) = SampleTemp, % I,P are local to findall sort(SampleTemp) = SampleSorted, Sample = [P : [I,P] in SampleSorted], length(Sample) = SampleSize, patternlength(PL), findall([P,I], p(I,P)) = Pattern, sort(Pattern) = Sorted, Links = new_list(PL), Links :: 1..SampleSize, order(Links), Sel = [E : [_,I] in Sorted, element(Links[I],Sample,E)], order(Sel), solve([ff],Links) => println('ANSWER'), foreach(I in 1..PL) printf("solution(%w,%w). ", Links[I],Sample[Links[I]]) end, nl. asp(_) => println('INCONSISTENT'), println('no.'). order([L1|T@[L2|_]]) => L1 #< L2, order(T). order(_) => true. test => asp( \$[ t(1,16), t(2,9), t(3,4), t(4,1), t(5,12), t(6,2), t(7,18), t(8,14), t(9,7), t(10,19), t(11,5), t(12,8), t(13,10), t(14,3), t(15,6), t(16,11), t(17,20), t(18,13), t(19,17), t(20,15), p(1,8), p(2,5), p(3,1), p(4,4), p(5,3), p(6,2), p(7,7), %p(8,6), patternlength(7)]). test0 => asp( \$[t(1,5),t(2,3),t(3,1),t(4,4),t(5,2), p(1,3),p(2,4),p(3,2), patternlength(3)]). test2 => asp( \$[t(1,10), t(2,25), t(3,9), t(4,50), t(5,3), t(6,12), t(7,14), t(8,20), t(9,45), t(10,41), t(11,8), t(12,1), t(13,40), t(14,16), t(15,39), t(16,29), t(17,36), t(18,44), t(19,46), t(20,21), t(21,18), t(22,35), t(23,4), t(24,7), t(25,32), t(26,11), t(27,42), t(28,49), t(29,37), t(30,19), t(31,22), t(32,33), t(33,24), t(34,43), t(35,5), t(36,15), t(37,13), t(38,26), t(39,34), t(40,23), t(41,28), t(42,27), t(43,38), t(44,48), t(45,2), t(46,17), t(47,6), t(48,31), t(49,30), t(50,47), p(1,1), p(2,14), p(3,15), p(4,3), p(5,5), p(6,4), p(7,8), p(8,10), p(9,13), p(10,7), p(11,9), p(12,6), p(13,12), p(14,2), p(15,11), patternlength(15) ]).