/
olympic.pi
76 lines (48 loc) · 1.41 KB
/
olympic.pi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Olympic puzzle in Picat.
Benchmark for Prolog (BProlog)
"""
File : olympic.pl
Author : Neng-Fa ZHOU
Date : 1993
Purpose: solve a puzzle taken from Olympic Arithmetic Contest
Given ten variables with the following configuration:
X7 X8 X9 X10
X4 X5 X6
X2 X3
X1
We already know that X1 is equal to 3 and want to assign each variable
with a different integer from {1,2,...,10} such that for any three
variables
Xi Xj
Xk
the following constraint is satisfied:
|Xi-Xj| = Xk
"""
Model created by Hakan Kjellerstrand, hakank@gmail.com
See also my Picat page: http://www.hakank.org/picat/
*/
import cp.
main => go.
go =>
_ = findall(_, ($olympic(Vars),$print_olympic(Vars))).
olympic(Vars) =>
Vars=[X1,X2,X3,X4,X5,X6,X7,X8,X9,X10],
Vars :: 1..10,
all_different(Vars),
X1 #= 3,
minus(X2,X3,X1),
minus(X4,X5,X2),
minus(X5,X6,X3),
minus(X7,X8,X4),
minus(X8,X9,X5),
minus(X9,X10,X6),
solve([ff], Vars).
print_olympic([X1,X2,X3,X4,X5,X6,X7,X8,X9,X10]) =>
printf("%d %d %d %d\n",X7,X8,X9,X10),
printf(" %d %d %d\n",X4,X5,X6),
printf(" %d %d\n",X2,X3),
printf(" %d\n",X1),
nl.
minus(X, Y, Z) =>
Z #= abs(X-Y).