Libraryless. Click here for Pure Java version (984L/7K/24K).
1 | !759 |
2 | |
3 | static final int gridSize = 10; |
4 | static int steps = 10; // steps limit |
5 | |
6 | sinterface Make { |
7 | X make(); |
8 | } |
9 | |
10 | asclass X { |
11 | abstract Move get(); |
12 | } |
13 | |
14 | asclass SelfMade extends X implements Make { |
15 | public X make() { start(); ret this; } |
16 | void start() {} // override me |
17 | } |
18 | |
19 | asclass Move { |
20 | } |
21 | |
22 | sclass ClearFixed extends Move { |
23 | int i; |
24 | |
25 | *() {} |
26 | *(int *i) {} |
27 | } |
28 | |
29 | sclass Grid { |
30 | int val = (1 << gridSize)-1; |
31 | |
32 | bool cleared() { ret val == 0; } |
33 | void clear(int i) { |
34 | if (i >= 0 && i < gridSize) |
35 | val &= ~(1 << i); |
36 | } |
37 | int remaining() { ret countBits(val); } |
38 | } |
39 | |
40 | static L<Make> pool; |
41 | |
42 | p { |
43 | pool = new L; |
44 | new Best<Make> best; |
45 | while (licensed()) { |
46 | ping(); |
47 | updatePool(); |
48 | |
49 | new HashMap<Make, Number> scores; |
50 | for (Make maker : pool) { |
51 | ping(); |
52 | int score = play(maker.make()); |
53 | scores.put(maker, score); |
54 | best.put(maker, score); |
55 | } |
56 | Make winner = lowest(scores); |
57 | print("Best score: " + scores.get(winner) + " - " + structure(winner) + ", all time: " + best.score); |
58 | } |
59 | } |
60 | |
61 | // collects LOWEST score |
62 | sclass Best<A> { |
63 | A best; |
64 | int score; |
65 | |
66 | void put(A a, int score) { |
67 | if (best == null || score < this.score) { |
68 | best = a; |
69 | this.score = score; |
70 | } |
71 | } |
72 | |
73 | A get() { ret best; } |
74 | } |
75 | |
76 | static int play(X x) { |
77 | new Grid g; |
78 | int i = 0; |
79 | while (i++ < steps && move(x, g) && !g.cleared()) {} |
80 | ret /*i +*/ g.remaining(); |
81 | } |
82 | |
83 | // false = x exited |
84 | static bool move(X x, Grid g) { |
85 | Move m = x.get(); |
86 | if (m == null) |
87 | ret false; |
88 | ClearFixed cf = cast m; |
89 | g.clear(cf.i); |
90 | ret true; |
91 | } |
92 | |
93 | //// CONTESTANTS //// |
94 | |
95 | sclass C1 extends SelfMade { |
96 | L<Integer> positions; |
97 | int i; |
98 | |
99 | *() {} |
100 | *(L<Integer> *positions) {} |
101 | *(int... l) { positions = asList(l); } |
102 | |
103 | void start() { i = 0; } |
104 | |
105 | Move get() { |
106 | if (i >= l(positions)) ret null; |
107 | ret new ClearFixed(positions.get(i++)); |
108 | } |
109 | } |
110 | |
111 | // updatePool |
112 | |
113 | static void updatePool() { |
114 | pool.clear(); |
115 | if (random(2) == 0) |
116 | pool.add(new C1(random(gridSize), random(gridSize))); |
117 | else |
118 | pool.add(new C1(random(gridSize), random(gridSize), random(gridSize))); |
119 | } |
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: | #1003133 |
Snippet name: | GenOpt pre 1 |
Eternal ID of this version: | #1003133/1 |
Text MD5: | 2528815b5b3bc573e96b9e71a25dba7e |
Transpilation MD5: | fcc4a8da247fe0822648f2f3224e3a0c |
Author: | stefan |
Category: | javax |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-05-14 00:23:16 |
Source code size: | 2195 bytes / 119 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 604 / 680 |
Referenced in: | [show references] |