-
Notifications
You must be signed in to change notification settings - Fork 1
/
swarm.py
1218 lines (930 loc) · 27 KB
/
swarm.py
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import math
import random
import time
import os
from threading import Thread
LOOP_LIMIT = 1000
SAFE_MODE = True
if SAFE_MODE:
MAX_INST = int('14E', 16)
else:
MAX_INST = int('168', 16)
class Live:
def __init__(self, filename):
self.variable = []
self.power_of_ten = 0
self.filename = filename
with open(filename) as f:
program = ''.join(line.strip() for line in f)
program = [program[i:i + 3] for i in range(0, len(program), 3)]
self.x000()
self.x000()
self.x000()
self.execute(list(program))
self.replicate(program)
# create a new variable
def x000(self):
self.variable.append(None)
# print all variables
def x001(self):
print(self.variable)
# assign an integer value
def x002(self):
self.variable[-1] = 0
# assign a float value
def x003(self):
self.variable[-1] = 0.0
# assign a string value
def x004(self):
self.variable[-1] = ""
# + 1
def x005(self):
self.variable[-1] = self.variable[-1] + 1 * (10 ** self.power_of_ten)
# - 1
def x006(self):
self.variable[-1] = self.variable[-1] - 1 * (10 ** self.power_of_ten)
# power of ten + 1
def x007(self):
self.power_of_ten = self.power_of_ten + 1
# power of ten - 1
def x008(self):
self.power_of_ten = self.power_of_ten - 1
# addition
def x009(self):
self.variable[-1] = self.variable[-1] + self.variable[-2]
# subtraction
def x00A(self):
self.variable[-1] = self.variable[-1] - self.variable[-2]
# multiplication
def x00B(self):
self.variable[-1] = self.variable[-1] * self.variable[-2]
# division
def x00C(self):
self.variable[-1] = self.variable[-1] / self.variable[-2]
# modulus
def x00D(self):
self.variable[-1] = self.variable[-1] % self.variable[-2]
# exponent
def x00E(self):
self.variable[-1] = self.variable[-1] ** self.variable[-2]
# floor division
def x00F(self):
self.variable[-1] = self.variable[-1] // self.variable[-2]
# assign False
def x010(self):
self.variable[-1] = False
# assign True
def x011(self):
self.variable[-1] = True
# == (equal)
def x012(self):
self.variable[-1] = self.variable[-1] == self.variable[-2]
# != (not equal)
def x013(self):
self.variable[-1] = self.variable[-1] != self.variable[-2]
# > (greater than)
def x014(self):
self.variable[-1] = self.variable[-1] > self.variable[-2]
# < (less than)
def x015(self):
self.variable[-1] = self.variable[-1] < self.variable[-2]
# >= (greater than or equal)
def x016(self):
self.variable[-1] = self.variable[-1] >= self.variable[-2]
# <= (less than or equal)
def x017(self):
self.variable[-1] = self.variable[-1] <= self.variable[-2]
# shift variables left
def x018(self):
self.variable = self.variable[1:] + [self.variable[0]]
# shift variables right
def x019(self):
self.variable = [self.variable[-1]] + self.variable[:-1]
# assign an empty list
def x01A(self):
self.variable[-1] = []
# assign an empty dictionary
def x01B(self):
self.variable[-1] = {}
# if
def x01C(self):
if self.variable[-1]:
return True
else:
return False
# if not
def x01D(self):
if not self.variable[-1]:
return True
else:
return False
# end
def x01E(self):
pass
# while
def x01F(self):
if self.variable[-1]:
return True
else:
return False
# unless
def x020(self):
if not self.variable[-1]:
return True
else:
return False
# break
def x021(self):
pass
# abs
def x022(self):
self.variable[-1] = abs(self.variable[-1])
# ceil
def x023(self):
self.variable[-1] = math.ceil(self.variable[-1])
# cmp
def x024(self):
self.variable[-1] = cmp(self.variable[-1], self.variable[-2])
# exp
def x025(self):
self.variable[-1] = math.exp(self.variable[-1])
# fabs
def x026(self):
self.variable[-1] = math.fabs(self.variable[-1])
# floor
def x027(self):
self.variable[-1] = math.floor(self.variable[-1])
# log
def x028(self):
self.variable[-1] = math.log(self.variable[-1])
# log10
def x029(self):
self.variable[-1] = math.log10(self.variable[-1])
# max
def x02A(self):
self.variable[-1] = max(self.variable[-1])
# min
def x02B(self):
self.variable[-1] = min(self.variable[-1])
# modf
def x02C(self):
self.variable[-1] = math.modf(self.variable[-1])
# pow
def x02D(self):
self.variable[-1] = math.pow(self.variable[-1], self.variable[-2])
# round
def x02E(self):
self.variable[-1] = round(self.variable[-1], self.variable[-2])
# sqrt
def x02F(self):
self.variable[-1] = math.sqrt(self.variable[-1])
# choice
def x030(self):
self.variable[-1] = random.choice(self.variable[-1])
# randrange
def x031(self):
self.variable[-1] = random.randrange(self.variable[-1], self.variable[-2], self.variable[-3])
# random
def x032(self):
self.variable[-1] = random.random()
# seed
def x033(self):
random.seed(self.variable[-1])
# shuffle
def x034(self):
random.shuffle(self.variable[-1])
# uniform
def x035(self):
self.variable[-1] = random.uniform(self.variable[-1], self.variable[-2])
# acos
def x036(self):
self.variable[-1] = math.acos(self.variable[-1])
# asin
def x037(self):
self.variable[-1] = math.asin(self.variable[-1])
# atan
def x038(self):
self.variable[-1] = math.atan(self.variable[-1])
# atan2
def x039(self):
self.variable[-1] = math.atan2(self.variable[-1], self.variable[-2])
# cos
def x03A(self):
self.variable[-1] = math.cos(self.variable[-1])
# hypot
def x03B(self):
self.variable[-1] = math.hypot(self.variable[-1], self.variable[-2])
# sin
def x03C(self):
self.variable[-1] = math.sin(self.variable[-1])
# tan
def x03D(self):
self.variable[-1] = math.tan(self.variable[-1])
# degrees
def x03E(self):
self.variable[-1] = math.degrees(self.variable[-1])
# radians
def x03F(self):
self.variable[-1] = math.radians(self.variable[-1])
# assign pi
def x040(self):
self.variable[-1] = math.pi
# assign e
def x041(self):
self.variable[-1] = math.e
# letter a
def x042(self):
self.variable[-1] = 'a'
# letter b
def x043(self):
self.variable[-1] = 'b'
# letter c
def x044(self):
self.variable[-1] = 'c'
# letter d
def x045(self):
self.variable[-1] = 'd'
# letter e
def x046(self):
self.variable[-1] = 'e'
# letter f
def x047(self):
self.variable[-1] = 'f'
# letter g
def x048(self):
self.variable[-1] = 'g'
# letter h
def x049(self):
self.variable[-1] = 'h'
# letter i
def x04A(self):
self.variable[-1] = 'i'
# letter j
def x04B(self):
self.variable[-1] = 'j'
# letter k
def x04C(self):
self.variable[-1] = 'k'
# letter l
def x04D(self):
self.variable[-1] = 'l'
# letter m
def x04E(self):
self.variable[-1] = 'm'
# letter n
def x04F(self):
self.variable[-1] = 'n'
# letter o
def x050(self):
self.variable[-1] = 'o'
# letter p
def x051(self):
self.variable[-1] = 'p'
# letter q
def x052(self):
self.variable[-1] = 'q'
# letter r
def x053(self):
self.variable[-1] = 'r'
# letter s
def x054(self):
self.variable[-1] = 's'
# letter t
def x055(self):
self.variable[-1] = 't'
# letter u
def x056(self):
self.variable[-1] = 'u'
# letter v
def x057(self):
self.variable[-1] = 'v'
# letter w
def x058(self):
self.variable[-1] = 'w'
# letter x
def x059(self):
self.variable[-1] = 'x'
# letter y
def x05A(self):
self.variable[-1] = 'y'
# letter z
def x05B(self):
self.variable[-1] = 'z'
# letter A
def x05C(self):
self.variable[-1] = 'A'
# letter B
def x05D(self):
self.variable[-1] = 'B'
# letter C
def x05E(self):
self.variable[-1] = 'C'
# letter D
def x05F(self):
self.variable[-1] = 'D'
# letter E
def x060(self):
self.variable[-1] = 'E'
# letter F
def x061(self):
self.variable[-1] = 'F'
# letter G
def x062(self):
self.variable[-1] = 'G'
# letter H
def x063(self):
self.variable[-1] = 'H'
# letter I
def x064(self):
self.variable[-1] = 'I'
# letter J
def x065(self):
self.variable[-1] = 'J'
# letter K
def x066(self):
self.variable[-1] = 'K'
# letter L
def x067(self):
self.variable[-1] = 'L'
# letter M
def x068(self):
self.variable[-1] = 'M'
# letter N
def x069(self):
self.variable[-1] = 'N'
# letter O
def x06A(self):
self.variable[-1] = 'O'
# letter P
def x06B(self):
self.variable[-1] = 'P'
# letter Q
def x06C(self):
self.variable[-1] = 'Q'
# letter R
def x06D(self):
self.variable[-1] = 'R'
# letter S
def x06E(self):
self.variable[-1] = 'S'
# letter T
def x06F(self):
self.variable[-1] = 'T'
# letter U
def x071(self):
self.variable[-1] = 'U'
# letter V
def x072(self):
self.variable[-1] = 'V'
# letter W
def x073(self):
self.variable[-1] = 'W'
# letter X
def x074(self):
self.variable[-1] = 'X'
# letter Y
def x075(self):
self.variable[-1] = 'Y'
# letter Z
def x076(self):
self.variable[-1] = 'Z'
# character 0
def x077(self):
self.variable[-1] = '0'
# character 1
def x078(self):
self.variable[-1] = '1'
# character 2
def x079(self):
self.variable[-1] = '2'
# character 3
def x07A(self):
self.variable[-1] = '3'
# character 4
def x07B(self):
self.variable[-1] = '4'
# character 5
def x07C(self):
self.variable[-1] = '5'
# character 6
def x07D(self):
self.variable[-1] = '6'
# character 7
def x07E(self):
self.variable[-1] = '7'
# character 8
def x07F(self):
self.variable[-1] = '8'
# character 9
def x080(self):
self.variable[-1] = '9'
# character "
def x081(self):
self.variable[-1] = '"'
# character !
def x082(self):
self.variable[-1] = '!'
# character '
def x083(self):
self.variable[-1] = "'"
# character ^
def x084(self):
self.variable[-1] = '^'
# character #
def x085(self):
self.variable[-1] = '#'
# character +
def x086(self):
self.variable[-1] = '+'
# character $
def x087(self):
self.variable[-1] = '$'
# character %
def x088(self):
self.variable[-1] = '%'
# character &
def x089(self):
self.variable[-1] = '&'
# character /
def x08A(self):
self.variable[-1] = '/'
# character {
def x08B(self):
self.variable[-1] = '{'
# character (
def x08C(self):
self.variable[-1] = '('
# character [
def x08D(self):
self.variable[-1] = '['
# character )
def x08E(self):
self.variable[-1] = ')'
# character ]
def x08F(self):
self.variable[-1] = ']'
# character =
def x090(self):
self.variable[-1] = '='
# character }
def x091(self):
self.variable[-1] = '}'
# character ?
def x092(self):
self.variable[-1] = '?'
# character *
def x093(self):
self.variable[-1] = '*'
# character \
def x094(self):
self.variable[-1] = '\\'
# character -
def x095(self):
self.variable[-1] = '-'
# character _
def x096(self):
self.variable[-1] = '_'
# character \t
def x097(self):
self.variable[-1] = '\t'
# character \n
def x098(self):
self.variable[-1] = '\n'
# character \r
def x099(self):
self.variable[-1] = '\r'
# character @
def x09A(self):
self.variable[-1] = '@'
# character ~
def x09B(self):
self.variable[-1] = '~'
# character `
def x09C(self):
self.variable[-1] = '`'
# character ,
def x09D(self):
self.variable[-1] = ','
# character ;
def x09E(self):
self.variable[-1] = ';'
# character <
def x09F(self):
self.variable[-1] = '<'
# character >
def x100(self):
self.variable[-1] = '>'
# character |
def x101(self):
self.variable[-1] = '|'
# character .
def x102(self):
self.variable[-1] = '.'
# character :
def x103(self):
self.variable[-1] = ':'
# capitalize
def x104(self):
self.variable[-1] = self.variable[-1].capitalize()
# string center
def x105(self):
self.variable[-1] = self.variable[-1].center(self.variable[-2], self.variable[-3])
# string/list count
def x106(self):
self.variable[-1] = self.variable[-1].count(self.variable[-2])
# string startswith
def x107(self):
self.variable[-1] = self.variable[-1].startswith(self.variable[-2])
# string endswith
def x108(self):
self.variable[-1] = self.variable[-1].endswith(self.variable[-2])
# string expandtabs
def x109(self):
self.variable[-1] = self.variable[-1].expandtabs(self.variable[-2])
# string find
def x10A(self):
self.variable[-1] = self.variable[-1].find(self.variable[-2])
# string/list index
def x10B(self):
self.variable[-1] = self.variable[-1].index(self.variable[-2])
# string isalnum
def x10C(self):
self.variable[-1] = self.variable[-1].isalnum()
# string isalpha
def x10D(self):
self.variable[-1] = self.variable[-1].isalpha()
# string isdigit
def x10E(self):
self.variable[-1] = self.variable[-1].isdigit()
# string islower
def x10F(self):
self.variable[-1] = self.variable[-1].islower()
# string isnumeric
def x110(self):
self.variable[-1] = self.variable[-1].isnumeric()
# string isspace
def x111(self):
self.variable[-1] = self.variable[-1].isspace()
# string istitle
def x112(self):
self.variable[-1] = self.variable[-1].istitle()
# string isupper
def x113(self):
self.variable[-1] = self.variable[-1].isupper()
# string join
def x114(self):
self.variable[-1] = self.variable[-2].join(self.variable[-1])
# length
def x115(self):
self.variable[-1] = len(self.variable[-2])
# string left justify
def x116(self):
self.variable[-1] = self.variable[-1].ljust(self.variable[-2], self.variable[-3])
# string lower
def x117(self):
self.variable[-1] = self.variable[-1].lower()
# string upper
def x118(self):
self.variable[-1] = self.variable[-1].upper()
# string left strip
def x119(self):
self.variable[-1] = self.variable[-1].lstrip(self.variable[-2])
# string replace
def x11A(self):
self.variable[-1] = self.variable[-1].replace(self.variable[-2], self.variable[-3])
# string right justify
def x11B(self):
self.variable[-1] = self.variable[-1].rjust(self.variable[-2], self.variable[-3])
# string right strip
def x11C(self):
self.variable[-1] = self.variable[-1].rstrip(self.variable[-2])
# string split
def x11D(self):
self.variable[-1] = self.variable[-1].split(self.variable[-2])
# string splitlines
def x11E(self):
self.variable[-1] = self.variable[-1].splitlines()
# string strip
def x11F(self):
self.variable[-1] = self.variable[-1].strip(self.variable[-2])
# string swapcase
def x120(self):
self.variable[-1] = self.variable[-1].swapcase()
# string title
def x121(self):
self.variable[-1] = self.variable[-1].title()
# string zero fill
def x122(self):
self.variable[-1] = self.variable[-1].zfill(self.variable[-2])
# string isdecimal
def x123(self):
self.variable[-1] = self.variable[-1].isdecimal()
# list index
def x124(self):
self.variable[-1] = self.variable[-1][self.variable[-2]]
# list slice
def x125(self):
self.variable[-1] = self.variable[-1][self.variable[-2]:self.variable[-3]]
# list index assign
def x126(self):
self.variable[-1][self.variable[-2]] = self.variable[-3]
# delete list element
def x127(self):
del self.variable[-1][self.variable[-2]]
# list append
def x128(self):
self.variable[-1].append(self.variable[-2])
# list extend
def x129(self):
self.variable[-1].extend(self.variable[-2])
# list insert
def x12A(self):
self.variable[-1].insert(self.variable[-2])
# list pop
def x12B(self):
self.variable[-1] = self.variable[-2].pop(self.variable[-3])
# list remove
def x12C(self):
self.variable[-1].remove(self.variable[-2])
# list reverse
def x12D(self):
self.variable[-1].reverse()
# list sort
def x12E(self):
self.variable[-1].sort()
# list shift left
def x12F(self):
self.variable[-1] = self.variable[-1][1:] + [self.variable[-1][0]]
# list shift right
def x130(self):
self.variable[-1] = [self.variable[-1][-1]] + self.variable[-1][:-1]
# list to tuple
def x131(self):
self.variable[-1] = tuple(self.variable[-1])
# convert to str
def x132(self):
self.variable[-1] = str(self.variable[-1])
# convert to int
def x133(self):
self.variable[-1] = int(self.variable[-1])
# convert to float
def x134(self):
self.variable[-1] = float(self.variable[-1])
# convert to long
def x135(self):
self.variable[-1] = long(self.variable[-1])
# convert to complex
def x136(self):
self.variable[-1] = complex(self.variable[-1], self.variable[-2])
# convert to repr
def x137(self):
self.variable[-1] = repr(self.variable[-1])
# convert to eval
def x138(self):
self.variable[-1] = eval(self.variable[-1])
# convert to list
def x139(self):
self.variable[-1] = list(self.variable[-1])
# convert to chr
def x13A(self):
self.variable[-1] = chr(self.variable[-1])
# convert to ord
def x13B(self):
self.variable[-1] = ord(self.variable[-1])
# convert to oct
def x13C(self):
self.variable[-1] = oct(self.variable[-1])
# not
def x13D(self):
self.variable[-1] = not self.variable[-1]
# and
def x13E(self):
self.variable[-1] = self.variable[-1] and self.variable[-2]
# or
def x13F(self):
self.variable[-1] = self.variable[-1] or self.variable[-2]
# dictionary assign
def x140(self):
self.variable[-1][str(self.variable[-2])] = self.variable[-3]
# dictionary delete
def x141(self):
del self.variable[-1][str(self.variable[-2])]
# dictionary clear
def x142(self):
self.variable[-1].clear()
# dictionary copy
def x143(self):
self.variable[-1] = self.variable[-2].copy()
# dictionary fromkeys
def x144(self):
self.variable[-1] = self.variable[-1].fromkeys(self.variable[-2])
# dictionary get
def x145(self):
self.variable[-1] = self.variable[-1].get(str(self.variable[-2]), self.variable[-3])
# dictionary has_key
def x146(self):
self.variable[-1] = self.variable[-1].has_key(str(self.variable[-2]))
# dictionary items
def x147(self):
self.variable[-1] = self.variable[-1].items()
# dictionary keys
def x148(self):
self.variable[-1] = self.variable[-1].keys()
# dictionary setdefault
def x149(self):
self.variable[-1] = self.variable[-1].setdefault(str(self.variable[-2]), self.variable[-3])
# dictionary update
def x14A(self):
self.variable[-1].clear(self.variable[-2])
# dictionary values
def x14B(self):
self.variable[-1] = self.variable[-1].values()
# time get ticks
def x14C(self):
self.variable[-1] = time.time()
# time get CPU time
def x14D(self):
self.variable[-1] = time.clock()
# time sleep/wait
def x14E(self):
#time.sleep(float(self.variable[-1])) TODO
pass
# Methods related to Files & Directories START
# It's dangerous to include these methods after this point
# file open