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

73
LINES

< > BotCompany Repo | #1004585 // Auto-generate Lua code to sort numbers [using 2 numbers, WORKS]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 9620K of libraries. Click here for Pure Java version (1676L/11K/39K).

!752

!include #1004584 // GenOpt 3

static final int numbers = 2; // how many numbers to sort

sclass Data {
  int[] numbers;
  Grid grid;
  
  *() {}
  *(int[] *numbers) {}
}

sclass MyGenOpt extends GenOpt<Data> {
  void fillSandbox(Sandbox s, Data d) {
    d.grid = new Grid(d.numbers);
    final Grid g = d.grid;
    
    s.setOuter("get", new OneArgFunction() {
      public LuaValue call(LuaValue arg) {
        ret Lua.value(g.get(arg.toint()));
      }
    });
  
    s.setOuter("swap", new TwoArgFunction() {
      public LuaValue call(LuaValue a, LuaValue b) {
        g.swap(a.toint(), b.toint());
        ret Lua.NIL;
      }
    });
  }
  
  double getScore(Sandbox s, Data data, LuaValue result) {
    int[] sorted = sortedArray(data.numbers);
    int errors = 0;
    for (int i = 0; i < numbers; i++)
      if (data.grid.get(i) != sorted[i])
        ++errors;
    ret 100-errors;
  }
  
  Data makeData() {
    int[] data = new int[numbers];
    for (int i = 0; i < numbers; i++)
      data[i] = random(100);
    ret new Data(data);
  }

  void updatePool(L<Make> pool) {
    pool.clear();
    pool.add(new Bubblesort);
  }
}

p {
  new MyGenOpt go;
  go.steps = numbers*numbers*10; // execution steps limit
  go.go();
}


//// CONTESTANTS ////

static O bubblesort_bot;

sclass Bubblesort implements Make {
  public S getLua() {
    if (bubblesort_bot == null)
      bubblesort_bot = hotwire(numbers == 2 ? "#1003167" : "#1003158");
    ret (S) call(bubblesort_bot, "makeLua");
  }
}

Author comment

Began life as a copy of #1003166

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1004585
Snippet name: Auto-generate Lua code to sort numbers [using 2 numbers, WORKS]
Eternal ID of this version: #1004585/1
Text MD5: 4f4e9de8e539ed141fb9b60cb41fd6f7
Transpilation MD5: e7c60ed1f39694e60442f9cbd8bd0a3f
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-23 15:11:05
Source code size: 1572 bytes / 73 lines
Pitched / IR pitched: No / No
Views / Downloads: 485 / 583
Referenced in: [show references]