Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

106
LINES

< > BotCompany Repo | #1003145 // Puzzler for Bubblesort making Lua (include, LIVE)

JavaX fragment (include)

1  
2  
sclass Puzzler {
3  
  new L<S> remainingTokens;
4  
  new L<S> numbers;
5  
  new L<S> vars;
6  
  bool aggressiveFirst = true;
7  
  bool useAllTokens = true;
8  
  int maxTries = 1000;
9  
  bool initVars = true;
10  
  S code = "";
11  
  
12  
  *(S tokens) {
13  
    for (S s : codeTokens(javaTok(tokens)))
14  
      if (eq(s, "i") || eq(s, "j")) {
15  
        vars.add(s);
16  
        if (initVars)
17  
          code += s + " = 0\n";
18  
      }
19  
      else if (isInteger(s))
20  
        numbers.add(s);
21  
      else
22  
        remainingTokens.add(s);
23  
  }
24  
  
25  
  S var() {
26  
    ret oneOf(vars);
27  
  }
28  
  
29  
  S exp() {
30  
    /*int i = random(2);
31  
    S s = "?";
32  
    switch (i) {
33  
      case 0: s = "0";
34  
      case 1: s = "9";
35  
    }*/
36  
    S s = oneOf(concatLists(numbers, vars));
37  
    
38  
    if (random(3) == 0) s = "get(" + s + ")";
39  
    ret s;
40  
  }
41  
  
42  
  S swap() {
43  
    ret "swap(" + exp() + ", " + exp() + ")\n";
44  
  }
45  
  
46  
  S _for() {
47  
    ret "for " + var() + " = " + exp() + ", " + exp() + "-1 do\n"
48  
      + indentx(code())
49  
      + "end\n";
50  
  }
51  
  
52  
  S ifless() {
53  
    ret "if " + exp() + " < " + exp() + " then\n" 
54  
      + indentx(code())
55  
      + "end\n";
56  
  }
57  
  
58  
  S code() {
59  
    S s = choose("swap", "for", "ifless");
60  
    if (s == null) ret "<OUT OF OPTIONS>";
61  
    ret (S) call(this, eq(s, "for") ? "_for" : s);
62  
    /*if (eq(s, "swap"))
63  
      ret swap();
64  
    if (eq(s, "for"))
65  
      ret for();*/
66  
  }
67  
  
68  
  S main() {
69  
    if (useAllTokens) ret code + code();
70  
    
71  
    L<S> tokens = cloneList(remainingTokens);
72  
    for (int i = 0; i < maxTries; i++) {
73  
      remainingTokens = cloneList(tokens);
74  
      S s = code();
75  
      if (empty(remainingTokens))
76  
        ret code + s;
77  
    }
78  
    throw fail("Can't solve using all tokens: " + structure(tokens));
79  
  }
80  
  
81  
  S choose(S... options) {
82  
    new L<S> l;
83  
    if (aggressiveFirst && nempty(remainingTokens) && contains(options, first(remainingTokens))) {
84  
      S s = first(remainingTokens);
85  
      remainingTokens.remove(0);
86  
      ret s;
87  
    }
88  
      
89  
    for (S s : options)
90  
      if (remainingTokens.contains(s))
91  
        l.add(s);
92  
    S choice = oneOf(l);
93  
    if (choice != null)
94  
      remainingTokens.remove(choice);
95  
    ret choice;
96  
  }
97  
}
98  
99  
/*
100  
for(variable, exp, exp, code) -> code
101  
ifless(exp, exp, code) -> code
102  
swap(exp, exp) -> code
103  
variable -> exp
104  
number -> exp
105  
get(exp) -> exp  // << always available, at least one level deep
106  
*/

Author comment

Began life as a copy of #1003143

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1003145
Snippet name: Puzzler for Bubblesort making Lua (include, LIVE)
Eternal ID of this version: #1003145/1
Text MD5: 7009560b8d5d57058ab794788fcdb727
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-05-15 22:50:00
Source code size: 2391 bytes / 106 lines
Pitched / IR pitched: No / No
Views / Downloads: 572 / 1318
Referenced in: [show references]