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)

sclass Puzzler {
  new L<S> remainingTokens;
  new L<S> numbers;
  new L<S> vars;
  bool aggressiveFirst = true;
  bool useAllTokens = true;
  int maxTries = 1000;
  bool initVars = true;
  S code = "";
  
  *(S tokens) {
    for (S s : codeTokens(javaTok(tokens)))
      if (eq(s, "i") || eq(s, "j")) {
        vars.add(s);
        if (initVars)
          code += s + " = 0\n";
      }
      else if (isInteger(s))
        numbers.add(s);
      else
        remainingTokens.add(s);
  }
  
  S var() {
    ret oneOf(vars);
  }
  
  S exp() {
    /*int i = random(2);
    S s = "?";
    switch (i) {
      case 0: s = "0";
      case 1: s = "9";
    }*/
    S s = oneOf(concatLists(numbers, vars));
    
    if (random(3) == 0) s = "get(" + s + ")";
    ret s;
  }
  
  S swap() {
    ret "swap(" + exp() + ", " + exp() + ")\n";
  }
  
  S _for() {
    ret "for " + var() + " = " + exp() + ", " + exp() + "-1 do\n"
      + indentx(code())
      + "end\n";
  }
  
  S ifless() {
    ret "if " + exp() + " < " + exp() + " then\n" 
      + indentx(code())
      + "end\n";
  }
  
  S code() {
    S s = choose("swap", "for", "ifless");
    if (s == null) ret "<OUT OF OPTIONS>";
    ret (S) call(this, eq(s, "for") ? "_for" : s);
    /*if (eq(s, "swap"))
      ret swap();
    if (eq(s, "for"))
      ret for();*/
  }
  
  S main() {
    if (useAllTokens) ret code + code();
    
    L<S> tokens = cloneList(remainingTokens);
    for (int i = 0; i < maxTries; i++) {
      remainingTokens = cloneList(tokens);
      S s = code();
      if (empty(remainingTokens))
        ret code + s;
    }
    throw fail("Can't solve using all tokens: " + structure(tokens));
  }
  
  S choose(S... options) {
    new L<S> l;
    if (aggressiveFirst && nempty(remainingTokens) && contains(options, first(remainingTokens))) {
      S s = first(remainingTokens);
      remainingTokens.remove(0);
      ret s;
    }
      
    for (S s : options)
      if (remainingTokens.contains(s))
        l.add(s);
    S choice = oneOf(l);
    if (choice != null)
      remainingTokens.remove(choice);
    ret choice;
  }
}

/*
for(variable, exp, exp, code) -> code
ifless(exp, exp, code) -> code
swap(exp, exp) -> code
variable -> exp
number -> exp
get(exp) -> exp  // << always available, at least one level deep
*/

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: 570 / 1316
Referenced in: [show references]