/
ch-2.ps
178 lines (166 loc) · 4.14 KB
/
ch-2.ps
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
%!PS
% begin included library code
% see https://codeberg.org/Firedrake/postscript-libraries/
/test {
/test.count test.count 1 add def
{
/test.pass test.pass 1 add def
} {
( ) print
test.count (....) cvs print
(-fail) print
} ifelse
} bind def
/deepcopy {
2 dict begin
/a exch def
a type /dicttype eq {
<<
a keys {
/k exch get
k
a k get deepcopy
} forall
>>
} {
a type /arraytype eq {
[
a {
deepcopy
} forall
]
} {
a type /stringtype eq {
a dup length string cvs
} {
a
} ifelse
} ifelse
} ifelse
end
} bind def
/apush.right { % [a b] c -> [a b c]
exch
[ exch aload length 2 add -1 roll ]
} bind def
/apop.right { % [a b c] -> [a b] c
[ exch aload length 1 add 1 roll ] exch
} bind def
/keys { % dict -> array of dict keys
[ exch
{
pop
} forall
]
} bind def
/remove {
2 dict begin
/i exch def
aload length /l exch def
l i sub -1 roll pop
l 1 sub array astore
end
} bind def
/test.start {
print (:) print
/test.pass 0 def
/test.count 0 def
} bind def
/test.end {
( ) print
test.count 0 gt {
(Passed ) print
test.pass (...) cvs print
(/) print
test.count (...) cvs print
( \() print
test.pass 100 mul test.count idiv (...) cvs print
(%\)) print
(\r\n) print
} if
} bind def
% end included library code
/collectpoints {
0 dict begin
/a exch def
/s 0 def
/m [
{
s a length ge {
exit
} if
/k a s get def
/e s def
{
e a length ge {
exit
} if
a e get k ne {
exit
} if
/e e 1 add def
} loop
[ k e s sub ]
/s e def
} loop
] def
/rv 0 def
/stack [ [ m 0 ] ] def
{
stack length 0 eq {
exit
} if
stack apop.right /s exch def /stack exch def
s 0 get length 0 eq {
/rv rv s 1 get max def
} {
0 1 s 0 get length 1 sub {
/i exch def
/ss s deepcopy def
ss 1 ss 1 get ss 0 get i get 1 get dup mul add put
ss 0 ss 0 get i remove put
{
/ex true def
i 1 add ss 0 get length lt {
ss 0 get i get 0 get ss 0 get i 1 add get 0 get eq {
ss 0 get i get 1
ss 0 get i get 1 get
ss 0 get i 1 add get 1 get
add
put
ss 0
ss 0 get i 1 add remove
put
/ex false def
} if
} if
ex {
i 0 gt i ss 0 get length lt and {
ss 0 get i get 0 get ss 0 get i 1 sub get 0 get eq {
ss 0 get i get 1
ss 0 get i get 1 get ss 0 get i 1 sub get 1 get add
put
ss 0
ss 0 get i 1 sub remove
put
/ex false def
} if
} if
} if
ex {
exit
} if
} loop
/stack stack ss apush.right def
} for
} ifelse
} loop
rv
end
} bind def
(collectpoints) test.start
[2 4 3 3 3 4 5 4 2] collectpoints 23 eq test
[1 2 2 2 2 1] collectpoints 20 eq test
[1] collectpoints 1 eq test
[2 2 2 1 1 2 2 2] collectpoints 40 eq test
test.end