!759 1003153 // with magic.jar // how many numbers to sort static final int numbers = 10; static int steps = numbers*numbers*10; // execution steps limit static int verifications = 1000; // sets of data to verify program with static bool useCheat = false; !include #1003139 // Assembly Machine sinterface Make { abstract S getLua(); } static L pool; static int round; static S verifiedProgram; p { pool = new L; new Best best; new Best bestProgram; while (licensed()) { ping(); ++round; updatePool(); new HashMap scores; int[] data = makeData(); for (Make maker : pool) { ping(); try { S program = maker.getLua(); if ((round % 10) == 0) print(program); int score = scoreProgram(program, data); scores.put(maker, score); best.put(maker, score); bestProgram.put(program, score); if (score == 0) { if (verifyProgram(program)) { print("Program verifies!"); verifiedProgram = program; print(program); ret; } else print("Program does not verify."); } } catch { // fail quietly } } Make winner = lowest(scores); if ((round % 10) == 0) print(round + " Best score: " + scores.get(winner) + " - " + structure(winner) + ", all time: " + best.score); if ((round % 100) == 0) print("All time winner: " + indent(bestProgram.best) + "\n"); } } // collects LOWEST score sclass Best { A best; int score; void put(A a, int score) { if (best == null || score < this.score) { best = a; this.score = score; } } A get() { ret best; } } static int getScore(Grid g, int[] data) { int[] sorted = sortedArray(data); int errors = 0; for (int i = 0; i < numbers; i++) //if (g.get(i) > g.get(i+1)) if (g.get(i) != sorted[i]) ++errors; ret errors; } static Sandbox makeSandbox(final Grid g) { Sandbox s = luaSandbox(); s.setInner("get", new OneArgFunction() { public LuaValue call(LuaValue arg) { ret Lua.value(g.get(arg.toint())); } }); s.setInner("swap", new TwoArgFunction() { public LuaValue call(LuaValue a, LuaValue b) { g.swap(a.toint(), b.toint()); ret Lua.NIL; } }); ret s; } static int scoreProgram(S program, int[] data) { Grid grid = new Grid(data); Sandbox sandbox = makeSandbox(grid); luaMaxSteps(steps); quickLua(sandbox, program); ret getScore(grid, data); } static bool verifyProgram(S program) { try { for (int i = 0; i < verifications; i++) if (scoreProgram(program, makeData()) != 0) ret false; ret true; } catch { ret false; } } // makeData static int[] makeData() { int[] data = new int[numbers]; for (int i = 0; i < numbers; i++) data[i] = random(100); ret data; } //// CONTESTANTS //// static O bubblesort_bot; sclass Bubblesort implements Make { public S getLua() { if (bubblesort_bot == null) bubblesort_bot = hotwire("#1003158"); ret (S) call(bubblesort_bot, "makeLua"); } } static class Cheat implements Make { public S getLua() { ret [[ for i = 0, 10-1 do for j = i, 10-1 do if get(j) < get(i) then swap(i, j) end end end ]]; } } // updatePool static void updatePool() { pool.clear(); pool.add(new Bubblesort); if (useCheat) pool.add(new Cheat); }