coctus 0.3.0

Command line tool for playing CodinGame puzzles and Clash of Code
Documentation
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
use clashlib::stub;

fn test_stub_builder(generator: &str, expected: &str) {
    let received = stub::generate("python", generator).unwrap().as_str().trim().to_string();
    let expected = expected.trim();

    assert_eq!(expected.lines().count(), received.lines().count());
    for (r, e) in expected.lines().zip(received.lines()) {
        assert_eq!(r, e)
    }
}

#[test]
fn test_stub_read_1() {
    let generator = r##"read anInt:int
read I:int
read aFloat:float
read aLong:long
read aString:string(256)
read aWord:word(256)
read    Spaces:string(10)     
read aBOOL:bool
"##;
    let expected = r##"an_int = int(input())
i = int(input())
a_float = float(input())
a_long = int(input())
a_string = input()
a_word = input()
spaces = input()
a_bool = input() != "0"
"##;

    test_stub_builder(generator, expected);
}

#[test]
fn test_stub_read_2() {
    let generator = r##"read x:int y:int
read x:int y:float
read two:word(50) words:word(50)
read aWord:word(50) x:int
read inputs:string(256)
"##;
    let expected = r##"x, y = [int(i) for i in input().split()]
inputs = input().split()
x = int(inputs[0])
y = float(inputs[1])
two, words = input().split()
inputs = input().split()
a_word = inputs[0]
x = int(inputs[1])
inputs = input()
"##;

    test_stub_builder(generator, expected);
}

#[test]
fn test_stub_loop() {
    let generator = r##"read nLoop:int
loop nLoop read anInt:int
loop nLoop read anInt:int aFloat:float aWord:word(256)
read nLoopLines:int
loopline nLoopLines anInt:int
loopline nLoopLines aFloat:float aLong:long
loop nLoop loopline 5 word:word(1)
loop nLoop loopline 5 string:string(1)
"##;
    let expected = r##"n_loop = int(input())
for i in range(n_loop):
    an_int = int(input())
for i in range(n_loop):
    inputs = input().split()
    an_int = int(inputs[0])
    a_float = float(inputs[1])
    a_word = inputs[2]
n_loop_lines = int(input())
for i in input().split():
    an_int = int(i)
inputs = input().split()
for i in range(n_loop_lines):
    a_float = float(inputs[2*i])
    a_long = int(inputs[2*i+1])
for i in range(n_loop):
    for word in input().split():
        pass
for i in range(n_loop):
    for j in input().split():
        string = j
"##;

    test_stub_builder(generator, expected);
}

#[test]
fn test_stub_write_1() {
    let generator = r##"write Never

read n:int
loop n write gonna

loop n loop n write let

write you
down

write write

write    care, here   spaces   everywhere    
    and some  more   

write and dont do this, this breaks paitong

write "
'
"
"##;
    let expected = r##"print("Never")
n = int(input())
for i in range(n):
    print("gonna")
for i in range(n):
    for j in range(n):
        print("let")
print("you")
print("down")
print("write")
print("care, here   spaces   everywhere")
print("and some  more")
print("and dont do this, this breaks paitong")
print(""")
print("'")
print(""")
"##;

    test_stub_builder(generator, expected);
}

#[test]
fn test_stub_write_2() {
    let generator = r##"read n:int
read aBc:int

write join("a", "b")
write join("a", n)
write join(n, "a")
write join(n, aBc)

write THIS IS IGNORED join(n, aBc)
write something join(n, aBc, n) something

write join(n, "potato", aBc, n)
"##;
    let expected = r##"n = int(input())
a_bc = int(input())
print("a b")
print("a " + str(n))
print(str(n) + " a")
print(str(n) + " " + str(a_bc))
print(str(n) + " " + str(a_bc))
print(str(n) + " " + str(a_bc) + " " + str(n))
print(str(n) + " potato " + str(a_bc) + " " + str(n))

"##;

    test_stub_builder(generator, expected);
}

#[test]
fn test_stub_write_3() {
    let generator = r##"read n:int
read aBc:int
write join(  "  le  "  ,  " spaces  "  )

write join(

write join (baited)
This only works because the next join gets parsed as raw text 
write join(       ) 

write join("a")
write join("b") join("IGNORED")
write join("c") join(

write join(join("a"), n, join("b")))))

write join("d", write("writeception"))
write join("d", write(join("a", aBc)))
write join("d", write join("a", aBc) )
"##;
    let expected = r##"n = int(input())
a_bc = int(input())
print("  le    spaces  ")
print("join(")
print("join (baited)")
print("This only works because the next join gets parsed as raw text")
print("write join(       )")
print("a")
print("b")
print("c")
print("a")
print("d writeception")
print("d a " + str(a_bc))
print("d a " + str(a_bc))

"##;

    test_stub_builder(generator, expected);
}

#[test]
fn test_stub_write_4() {
    let generator = r##"read NONSENSE:int
write join("hi", NONSENSE INJECTED "it's me Jim" WHAT THE F$)
write join("hi", NONSENSE, INJECTED "it's me Jim" WHAT THE F$)
write join("hi",,, "Jim")

write join(join("hi" , (((( "Jim") )

write join("NEVER") IGNORED join("GONNA")
write join() IGNORED join("GONNA")
write join() LET

write join("YOU", join("JOIN"))
"##;
    let expected = r##"nonsense = int(input())
print("hi it's me Jim")
print("hi " + str(nonsense) + " it's me Jim")
print("join("hi",,, "Jim")")
print("hi Jim")
print("NEVER")
print("GONNA")
print("join() LET")
print("YOU JOIN")
"##;

    test_stub_builder(generator, expected);
}

#[test]
fn test_stub_statement() {
    let generator = r##"STATEMENT
There can only be one per stub

STATEMENT this gets ignored
This should start here
STATEMENT
override the previous statement
     and end here (no spaces both sides)   


read n:int
loop n loop n loop n write crazy
right?
"##;
    let expected = r##"# This should start here
# STATEMENT
# override the previous statement
# and end here (no spaces both sides)

n = int(input())
for i in range(n):
    for j in range(n):
        for k in range(n):
            print("crazy")
            print("right?")
"##;

    test_stub_builder(generator, expected);
}

#[test]
fn test_stub_output() {
    let generator = r##"write answer0

write answer1

OUTPUT
This goes to answer 0 and 1
OUTPUT
Together with this

OUTPUT
THis goes to Narnia

write answer2

OUTPUT       oops!
This goes to answer 2 but not 3

write answer3
STATEMENT     
baited,     care spaces

STATEMENT
Hello world!
"##;
    let expected = r##"# Hello world!

# This goes to answer 0 and 1
# OUTPUT
# Together with this
print("answer0")
# This goes to answer 0 and 1
# OUTPUT
# Together with this
print("answer1")
# This goes to answer 2 but not 3
print("answer2")
print("answer3")
print("STATEMENT")
print("baited,     care spaces")
"##;

    test_stub_builder(generator, expected);
}

#[test]
fn test_stub_input_1() {
    let generator = r##"read init:int
read x:int

INPUT   ignored
x: some variable
y: some other inexistent variable

read x:int

INPUT
y: some variable that now exists but gets ignored due to previous INPUT

read two:word(50) words:word(50)
read i:int f:float

INPUT
init: the first variable
words:         some words

INPUT
two:a number, duh
i: int
f: float
"##;
    let expected = r##"init = int(input())  # the first variable
x = int(input())  # some variable
x = int(input())
# two: a number, duh
# words: some words
two, words = input().split()
inputs = input().split()
i = int(inputs[0])  # int
f = float(inputs[1])  # float
"##;

    test_stub_builder(generator, expected);
}

#[test]
fn test_stub_input_2() {
    let generator = r##"read A:int

INPUT case sensitive???
a : NEIN NEIN
A : ????
"##;

    let expected = r##"a = int(input())  # ????
"##;

    test_stub_builder(generator, expected);
}

#[test]
fn test_stub_everything() {
    let generator = r##"write many  spaces   here

read L:string(20)

OUTPUT
The spacemaster

read a:word(50) b:word(50)
read aBc:string(256)
read ROW:string(1024)

INPUT
ROW: Your boat
This is ignored
aBc: The alphabet

loop N read EXT:word(100) MT:word(100)
loop N read count:int name:word(50)

loop Q read FNAME:string(500)

loop 4 read number:int

loop 4 write 0 0

STATEMENT
Head, shoulders knees and toes
Knees and toes

read xCount:int
loopline xCount x:int
loopline xCount y:int z:word(50)
"##;
    let expected = r##"# Head, shoulders knees and toes
# Knees and toes

# The spacemaster
print("many  spaces   here")
l = input()
a, b = input().split()
a_bc = input()  # The alphabet
row = input()  # Your boat
for i in range(n):
    ext, mt = input().split()
for i in range(n):
    inputs = input().split()
    count = int(inputs[0])
    name = inputs[1]
for i in range(q):
    fname = input()
for i in range(4):
    number = int(input())
for i in range(4):
    print("0 0")
x_count = int(input())
for i in input().split():
    x = int(i)
inputs = input().split()
for i in range(x_count):
    y = int(inputs[2*i])
    z = inputs[2*i+1]
"##;

    test_stub_builder(generator, expected);
}

#[test]
fn test_stub_loops_spaces_and_newlines() {
    let generator = r##"read n:int
loop  
  n    
    

  loop 4 
write thing

loop n    
  loop 4      
  loopline
n x:int"##;
    let expected = r##"n = int(input())
for i in range(n):
    for j in range(4):
        print("thing")
for i in range(n):
    for j in range(4):
        for k in input().split():
            x = int(k)
"##;
    test_stub_builder(generator, expected);
}

#[test]
fn test_stub_variable_length() {
    let generator = r##"read n:int
read k:string(n)
loop n read a:string(5)
write answer

INPUT
k: this string is n-sized (irrelevant in python but be wary!)
"##;
    let expected = r##"n = int(input())
k = input()  # this string is n-sized (irrelevant in python but be wary!)
for i in range(n):
    a = input()
print("answer")
"##;

    test_stub_builder(generator, expected);
}

#[test]
fn test_stub_summary() {
    let generator = r##"read anInt:int
read aFloat:float
read Long:long
read aWord:word(1)
read boolean:bool
read ABC1ABc1aBC1AbC1abc1:int
read STRING:string(256)
read anInt2:int aFloat2:float Long2:long aWord2:word(1) boolean2:bool
loop anInt read x:int
loop anInt read x:int f:float
loop anInt loop anInt read x:int y:int
loopline anInt x:int
loopline anInt w:word(50)
loopline anInt x:int f:float w:word(50)
write result

OUTPUT
An output comment

write join(anInt, aFloat, Long, boolean)

write join(aWord, "literal", STRING)

STATEMENT
This is the statement

INPUT
anInt: An input comment over anInt
"##;
    let expected = r##"# This is the statement

an_int = int(input())  # An input comment over anInt
a_float = float(input())
long = int(input())
a_word = input()
boolean = input() != "0"
abc1abc_1a_bc1ab_c1abc_1 = int(input())
string = input()
inputs = input().split()
an_int_2 = int(inputs[0])
a_float_2 = float(inputs[1])
long_2 = int(inputs[2])
a_word_2 = inputs[3]
boolean_2 = inputs[4] != "0"
for i in range(an_int):
    x = int(input())
for i in range(an_int):
    inputs = input().split()
    x = int(inputs[0])
    f = float(inputs[1])
for i in range(an_int):
    for j in range(an_int):
        x, y = [int(k) for k in input().split()]
for i in input().split():
    x = int(i)
for w in input().split():
    pass
inputs = input().split()
for i in range(an_int):
    x = int(inputs[3*i])
    f = float(inputs[3*i+1])
    w = inputs[3*i+2]
# An output comment
print("result")
print(str(an_int) + " " + str(a_float) + " " + str(long) + " " + str(boolean))
print(a_word + " literal " + string)
"##;

    test_stub_builder(generator, expected);
}