-
Notifications
You must be signed in to change notification settings - Fork 0
/
dance.yuk
executable file
·201 lines (159 loc) · 4.04 KB
/
dance.yuk
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/awful
:set &prognum i0
:set &dances i0
:set &arg i0
!while :lt &arg :param-num
!if :eq s'--programs' :param-str &arg
:add &arg i1
:set &prognum :param-str &arg
!fi
!if :eq s'--dances' :param-str &arg
:add &arg i1
:set &dances :param-str &arg
!fi
:add &arg i1
!done
!if :le &dances i0
:writeln s'You must specify the number of dances: "--dances X"'
:exit
!fi
!if :le &prognum i0
:writeln s'You must specify the number of programs dancing: "--programs X"'
:exit
!else
:set &p i0
:set &prog :arr
!while :lt &p &prognum
:set &prog[&p] :chr :add i97 &p
:add &p i1
!done
!fi
:set &danceHistory :arr
:set &danceHistory[i0] :str-implode &prog sss
!fun :already-seen-before
:set ¤t :str-implode &prog sss
:set &hlen :arr-count &danceHistory
:set &hpos i0
!while :lt &hpos &hlen
!if :eq ¤t &danceHistory[i0]
:return :sub &hlen &hpos
!fi
:add &hpos i1
!done
:return i0
!nuf
!fun :dance-spin $num
:set &num :sub $prognum $num
:set © &prog
:set &p i0
!while :lt &p &prognum
:set &prog[&p] ©[:mod (:add $p $num) &prognum]
:add &p i1
!done
!nuf
!fun :dance-exchange $pos-a $pos-b
:set &temp &prog[&pos-a]
:set &prog[&pos-a] &prog[&pos-b]
:set &prog[&pos-b] &temp
!nuf
!fun :dance-partner $prog-a $prog-b
:set &pos-a &pos-b i0
!while :neq &prog[&pos-a] &prog-a
:add &pos-a i1
!done
!while :neq &prog[&pos-b] &prog-b
:add &pos-b i1
!done
:dance-exchange &pos-a &pos-b
!nuf
!fun :dance-permutate $permpos
:set © &prog
# :writeln s'> P' (:str-implode $permpos s'/') s' : ' :str-implode &prog sss
:set &p i0
!while :lt &p &prognum
# :writeln s'> moving ' ©[&p] s' from position ' &p s' to ' :mod (:add $p $permpos[$p]) $prognum
:set &prog[:mod (:add $p $permpos[:add i1 $p]) $prognum] ©[&p]
:add &p i1
!done
!nuf
:set &sparseInput :arr
:set &stepsSincePartner i0
:set &lastPartnerPos &prog
!fun :remember-permutation
!if :eq &stepsSincePartner i0
:return
!fi
# :writeln s'> ' (:str-implode &lastPartnerPos sss) s' vs ' (:str-implode &prog sss)
:set &permut :arr s'P'
:set &po i0
!while :lt &po &prognum
:set &pn i0
!while :neq &lastPartnerPos[&po] &prog[:mod (:add $po $pn) &prognum]
# :writeln s'> ' &lastPartnerPos[&po] s' vs ' &prog[:mod (:add $po $pn) &prognum] s' (' $prognum s')'
:add &pn i1
!done
:set &permut[:add i1 &po] &pn
:add &po i1
!done
:set &sparseInput[:arr-count &sparseInput] &permut
!nuf
:set &input-line s''
:readln &input-line
:set &input :str-explode &input-line s','
:set &idx i0
:set &len :arr-count &input
!while :lt &idx &len
:set &cmd &input[&idx][i1]
:set &args :str-sub &input[&idx] i2
# :write s'> ' &input[&idx] s': ' :str-implode &prog sss
!if :eq &cmd s's'
:dance-spin &args
:add &stepsSincePartner i1
!elif :eq &cmd s'x'
:set &args-arr :str-explode &args s'/'
:dance-exchange &args-arr[i0] &args-arr[i1]
:add &stepsSincePartner i1
!elif :eq &cmd s'p'
:set &args-arr :str-explode &args s'/'
:remember-permutation
:set &sparseInput[:arr-count &sparseInput] :arr s'p' &args-arr[i0] &args-arr[i1]
:dance-partner &args-arr[i0] &args-arr[i1]
:set &lastPartnerPos &prog
:set &stepsSincePartner i0
!fi
# :writeln s' -> ' :str-implode &prog sss
:add &idx i1
!done
:remember-permutation
:writeln :str-implode &prog sss
:set &danceHistory[i0] :str-implode &prog sss
#~
:set &si i0
!while :lt $si :arr-count &sparseInput
:writeln s'---> ' :str-implode &sparseInput[$si] s'/'
:add &si i1
!done
~#
:sub &dances i1
!while :gt &dances i0
:sub &dances i1
:set &idx i0
:set &len :arr-count &sparseInput
!while :lt &idx &len
:set &cmd &sparseInput[&idx][i0]
!if :eq &cmd s'p'
:dance-partner &sparseInput[&idx][i1] &sparseInput[&idx][i2]
!elif :eq &cmd s'P'
:dance-permutate &sparseInput[&idx]
!fi
:add &idx i1
!done
:writeln :str-implode &prog sss
:set &cycleLen :already-seen-before
!if :gt &cycleLen i0
:writeln s'-> cycle: ' $cycleLen
:set &dances :mod $dances $cycleLen
!else
:set &danceHistory[:arr-count &danceHistory] :str-implode &prog sss
!fi
!done