1 | !636
|
2 | !quickmain
|
3 | !auto-import
|
4 | !standard functions
|
5 | !quicknew
|
6 | !1000346 // use "case" as a variable name
|
7 | !688 // buf.isEmpty
|
8 | !class JavaTok
|
9 |
|
10 | import java.lang.reflect.*;
|
11 |
|
12 | interface Function {
|
13 | public Object process(Object in);
|
14 | public void toJava_process(Code code);
|
15 | }
|
16 |
|
17 | interface ReversibleFunction extends Function {
|
18 | public Object unprocess(Object in);
|
19 | public void toJava_unprocess(Code code);
|
20 | }
|
21 |
|
22 | // generic learner (works on objects)
|
23 | interface Learner {
|
24 | public void processInOut(Object in, Object out);
|
25 | public Object processIn(Object in);
|
26 | public void toJava(Code code);
|
27 | }
|
28 |
|
29 | abstract class Base {
|
30 | void printVars() {
|
31 | new StringBuilder buf;
|
32 | Field[] fields = getClass().getDeclaredFields();
|
33 | for (Field field : fields) {
|
34 | if ((field.getModifiers() & Modifier.STATIC) != 0)
|
35 | continue;
|
36 | Object value;
|
37 | try {
|
38 | value = field.get(this);
|
39 | } catch (Exception e) {
|
40 | value = "?";
|
41 | }
|
42 |
|
43 | if (!buf.isEmpty()) buf.append(", ");
|
44 | buf.append(field.getName() + "=" + value);
|
45 | }
|
46 | System.out.println(buf.toString());
|
47 | }
|
48 | }
|
49 |
|
50 | abstract class LearnerImpl extends Base implements Learner {
|
51 | }
|
52 |
|
53 | class Code {
|
54 | StringBuilder buf = new StringBuilder();
|
55 | String var = "in";
|
56 | String indent = "";
|
57 | List<String> translators = new ArrayList<String>();
|
58 |
|
59 | void line(String line) {
|
60 | buf.append(indent).append(line).append('\n');
|
61 | }
|
62 |
|
63 | void indent() {
|
64 | indent += " ";
|
65 | }
|
66 |
|
67 | void unindent() {
|
68 | indent = indent.substring(0, indent.length()-2);
|
69 | }
|
70 |
|
71 | void translators(String... ids) {
|
72 | for (String id : ids)
|
73 | if (! translators.contains(id)) // space is needed otherwise translator 636 is fooled :)
|
74 | translators.add(id);
|
75 | }
|
76 |
|
77 | String getTranslators() {
|
78 | return main.fromLines(translators);
|
79 | }
|
80 | }
|
81 |
|
82 | main {
|
83 | static List<Case> cases = new ArrayList<Case>();
|
84 |
|
85 | psvm {
|
86 | String snippetID = null;
|
87 |
|
88 | for (int i = 0; i < args.length; i++) {
|
89 | String arg = args[i];
|
90 | if (arg.equals("debug"))
|
91 | debugOn(args[++i]);
|
92 | else if (isSnippetID(arg))
|
93 | snippetID = arg;
|
94 | else
|
95 | System.err.println("Unknown argument: " + arg + ", ignoring");
|
96 | }
|
97 |
|
98 | parse(snippetID);
|
99 |
|
100 | int solved = 0, n = cases.size();
|
101 | for (int i = 0; i < n; i++) {
|
102 | calculate(cases.get(i));
|
103 | if (cases.get(i).winner != null)
|
104 | ++solved;
|
105 | System.out.println((i+1) + " case(s) processed, " + solved + " solved.");
|
106 | }
|
107 |
|
108 | print ""
|
109 | print "----"
|
110 |
|
111 | boolean allSolved = solved == n;
|
112 | if (!allSolved) {
|
113 | System.out.println();
|
114 | System.out.print("Unsolved: ");
|
115 | for (Case case : cases)
|
116 | if (case.winner == null)
|
117 | System.out.print(case.id + " ");
|
118 | System.out.println();
|
119 | }
|
120 | if (solved != 0) {
|
121 | System.out.println();
|
122 | System.out.print("Solved: ");
|
123 | for (Case case : cases)
|
124 | if (case.winner != null)
|
125 | System.out.print(case.id + " ");
|
126 | System.out.println();
|
127 | }
|
128 | System.out.println();
|
129 | System.out.println(allSolved ? "ALL SOLVED (" + solved + ")" : "Solved " + solved + " out of " + n + ".");
|
130 | print ""
|
131 | }
|
132 |
|
133 | static class Case {
|
134 | String id;
|
135 | List<String[]> fullExamples = new ArrayList<String[]>();
|
136 | List<String> halfExamples = new ArrayList<String>();
|
137 | List<String[]> examples1, examples2;
|
138 | Learner winner;
|
139 | }
|
140 |
|
141 | static void parse(String arg) tex {
|
142 | Case case = new Case();
|
143 | String text;
|
144 | if (arg != null) {
|
145 | case.id = arg;
|
146 | text = loadSnippet(arg);
|
147 | } else {
|
148 | case.id = "input.txt";
|
149 | text = loadTextFile("input/input.txt", null);
|
150 | if (text == null) {
|
151 | //case.id = "#2000455"; // example input
|
152 | case.id = "#681"; // a collection of "all cases"!
|
153 | text = loadSnippet(case.id);
|
154 | }
|
155 | }
|
156 |
|
157 | // it's a collection of cases!
|
158 | if (text.trim().startsWith("#")) {
|
159 | for (String line : toLines(text))
|
160 | parse(line);
|
161 | return;
|
162 | }
|
163 |
|
164 | System.out.println(text);
|
165 | String in = null, out = null;
|
166 |
|
167 | for (String line : toLines(text)) {
|
168 | if (line.startsWith("I")) { // "In: " or "I: "
|
169 | if (in != null)
|
170 | case.halfExamples.add(in);
|
171 | in = unquote(line.substring(line.indexOf(':')+1).trim());
|
172 | out = null;
|
173 | } else if (line.startsWith("O")) { // "Out: " or "O: "
|
174 | out = unquote(line.substring(line.indexOf(':')+1).trim());
|
175 | System.out.println(quote(in) + " => " + quote(out));
|
176 | case.fullExamples.add(new String[] {in, out});
|
177 | in = out = null;
|
178 | }
|
179 | }
|
180 |
|
181 | if (in != null)
|
182 | case.halfExamples.add(in);
|
183 | cases.add(case);
|
184 | }
|
185 |
|
186 | static void calculate(Case case) tex {
|
187 | if (case.fullExamples.size() < 2)
|
188 | throw new RuntimeException("Too few examples (" + case.fullExamples.size() + ")");
|
189 | int splitPoint = case.fullExamples.size()-1;
|
190 | System.out.println("Full examples: " + case.fullExamples.size() + ", splitPoint: " + splitPoint);
|
191 | case.examples1 = case.fullExamples.subList(0, splitPoint);
|
192 | case.examples2 = case.fullExamples.subList(splitPoint, case.fullExamples.size());
|
193 |
|
194 | Learner learner = findOKLearner(case);
|
195 | if (learner == null)
|
196 | print "\nProblem not solved"
|
197 | else {
|
198 | print "\nSolved!"
|
199 | case.winner = learner;
|
200 | Code code = new Code();
|
201 | learner.toJava(code);
|
202 | System.out.println("Java:");
|
203 | System.out.println(indent(" ", code.getTranslators()));
|
204 | System.out.println(indent(" ", code.buf.toString()));
|
205 | for (String in : case.halfExamples) {
|
206 | Object out = learner.processIn(in);
|
207 | System.out.println(quote(in) + " =>! " + quote((String) out));
|
208 | }
|
209 | }
|
210 | }
|
211 |
|
212 | static Learner findOKLearner(Case case) {
|
213 | for (Learner learner : makeLearners()) try {
|
214 | if (learnerOK(learner, case))
|
215 | return learner;
|
216 | } catch (Throwable e) {
|
217 | e.printStackTrace();
|
218 | }
|
219 | return null;
|
220 | }
|
221 |
|
222 | static boolean learnerOK(Learner learner, Case case) {
|
223 | String[] _e = null;
|
224 | try {
|
225 | for (String[] e : case.examples1) {
|
226 | _e = e;
|
227 | learner.processInOut(e[0], e[1]);
|
228 | }
|
229 |
|
230 | // full validation against all examples
|
231 | for (String[] e : case.fullExamples) {
|
232 | _e = e;
|
233 | String out = (String) learner.processIn(e[0]);
|
234 | if (!e[1].equals(out)) {
|
235 | System.out.println("[fail] " + learner + " on " + quote(e[0]) + " - got: " + quote(out) + " rather than: " + quote(e[1]));
|
236 | return false;
|
237 | }
|
238 | }
|
239 | return true; // all test examples passed
|
240 | } catch (Throwable e) {
|
241 | System.out.println("[fail] " + learner + " on " + (_e == null ? "?" : quote(_e[0])) + " - " + e);
|
242 | silentException(e);
|
243 | return false;
|
244 | }
|
245 | }
|
246 |
|
247 | static void silentException(Throwable e) {
|
248 | }
|
249 |
|
250 | static Iterable<Learner> makeLearners() {
|
251 | List<Learner> list = new ArrayList<Learner>();
|
252 | //list.add(new LId()); // subsumed by trivial case of PrefixSuffix
|
253 | list.add(new LPrefixSuffix());
|
254 | list.add(new LSplitInput(new LOutPattern()));
|
255 | list.add(new LInputPattern());
|
256 | list.add(new LFixed());
|
257 | list.add(new LBoth(new RFJavaTok(), new LEach(new LFixedFunction(new EscapeCase()))));
|
258 | return list;
|
259 | }
|
260 |
|
261 | public static String unquote(String s) {
|
262 | if (s.startsWith("\"") && s.endsWith("\"") && s.length() > 1)
|
263 | return s.substring(1, s.length()-1).replace("\\\"", "\"").replace("\\\\", "\\"); // SHOULD work...
|
264 | else
|
265 | return s; // Return SOMETHING
|
266 | }
|
267 |
|
268 | public static String quote(String s) {
|
269 | if (s == null) return "null";
|
270 | return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
|
271 | }
|
272 |
|
273 | static String indent(String indent, String s) {
|
274 | return indent + s.replace("\n", "\n" + indent);
|
275 | }
|
276 |
|
277 | static void debugOn(String name) {
|
278 | try {
|
279 | Class c = Class.forName("main$" + name);
|
280 | Field field;
|
281 | while (true)
|
282 | try {
|
283 | field = c.getDeclaredField("debug");
|
284 | break;
|
285 | } catch (NoSuchFieldException e) {
|
286 | c = c.getSuperclass();
|
287 | }
|
288 | field.setBoolean(null, true);
|
289 | } catch (Exception e) {
|
290 | e.printStackTrace();
|
291 | System.err.println("Cannot debug class " + name);
|
292 | }
|
293 | }
|
294 |
|
295 | // splits the input at some point, takes only one part
|
296 | static class LSplitInput implements Learner {
|
297 | int splitIdx = 1; // split after first character
|
298 | Learner baseLearner;
|
299 |
|
300 | LSplitInput(Learner baseLearner) {
|
301 | this.baseLearner = baseLearner;
|
302 | }
|
303 |
|
304 | public void processInOut(Object _in, Object _out) {
|
305 | String in = (String) _in, out = (String) _out;
|
306 | in = in.substring(splitIdx);
|
307 | baseLearner.processInOut(in, out);
|
308 | }
|
309 |
|
310 | public Object processIn(Object _in) {
|
311 | String in = (String) _in;
|
312 | in = in.substring(splitIdx);
|
313 | return baseLearner.processIn(in);
|
314 | }
|
315 |
|
316 | public void toJava(Code code) {
|
317 | code.line(code.var + " = ((String) " + code.var + ").substring(" + splitIdx + ");");
|
318 | baseLearner.toJava(code);
|
319 | }
|
320 | }
|
321 |
|
322 | // if input appears in output in fixed pattern
|
323 | static class LOutPattern implements Learner {
|
324 | String pattern = "%!%";
|
325 |
|
326 | public void processInOut(Object _in, Object _out) {
|
327 | String in = (String) _in, out = (String) _out;
|
328 | pattern = out.replace(in, "%!%");
|
329 | }
|
330 |
|
331 | public String processIn(Object _in) {
|
332 | String in = (String) _in;
|
333 | return pattern.replace("%!%", in);
|
334 | }
|
335 |
|
336 | public void toJava(Code code) {
|
337 | code.line(code.var + " = " + quote(pattern) + ".replace(" + quote("%!%") + ", (String) " + code.var + ");");
|
338 | }
|
339 | }
|
340 |
|
341 | // learns to exchange common prefixes and suffixes
|
342 | static class LPrefixSuffix extends LearnerImpl {
|
343 | static boolean debug;
|
344 | String prefixIn, suffixIn, prefixOut, suffixOut;
|
345 |
|
346 | public void processInOut(Object _in, Object _out) {
|
347 | String in = (String) _in, out = (String) _out;
|
348 | updateIn(in);
|
349 | prefixOut = prefixOut == null ? out : commonPrefix(prefixOut, out);
|
350 | suffixOut = suffixOut == null ? out : commonSuffix(suffixOut, out);
|
351 | if (debug)
|
352 | printState("processInOut(" + quote(in) + ", " + quote(out) + ")");
|
353 | }
|
354 |
|
355 | void updateIn(String in) {
|
356 | prefixIn = prefixIn == null ? in : commonPrefix(prefixIn, in);
|
357 | suffixIn = suffixIn == null ? in : commonSuffix(suffixIn, in);
|
358 | if (debug)
|
359 | printState("updateIn(" + quote(in) + ")");
|
360 | }
|
361 |
|
362 | public String processIn(Object _in) {
|
363 | String in = (String) _in;
|
364 | //System.out.println("[before last info] " + quote(prefixIn) + " " + quote(suffixIn) + " " + quote(prefixOut) + " " + quote(suffixOut));
|
365 | //System.out.println("[last info] " + quote(in));
|
366 |
|
367 | // use latest information
|
368 | String p = prefixIn, s = suffixIn;
|
369 | updateIn(in);
|
370 | prefixOut = prefixOut.substring(0, prefixOut.length()-(p.length()-prefixIn.length()));
|
371 | suffixOut = suffixOut.substring(s.length()-suffixIn.length());
|
372 |
|
373 | //System.out.println("[after last info] " + quote(prefixIn) + " " + quote(suffixIn) + " " + quote(prefixOut) + " " + quote(suffixOut));
|
374 | String core = in.substring(prefixIn.length(), in.length()-suffixIn.length());
|
375 | return prefixOut + core + suffixOut;
|
376 | }
|
377 |
|
378 | public void toJava(Code code) {
|
379 | code.line(code.var + " = ((String) " + code.var + ").substring(" + prefixIn.length() + ", in.length()-" + suffixIn.length() + ");");
|
380 | code.line(code.var + " = " + quote(prefixOut) + " + " + code.var + " + " + quote(suffixOut) + ";");
|
381 | }
|
382 |
|
383 | void printState(String text) {
|
384 | System.out.println(text);
|
385 | printVars();
|
386 | }
|
387 | }
|
388 |
|
389 | // for "find" tasks (e.g. "abcde" to "[[abc]]de")
|
390 | static class LInputPattern implements Learner {
|
391 | String regexp = "";
|
392 |
|
393 | public void processInOut(Object _in, Object _out) {
|
394 | String in = (String) _in, out = (String) _out;
|
395 | int i = out.indexOf("[["), j = out.indexOf("]]", i+1);
|
396 | if (j < 0) return;
|
397 | String s = out.substring(i+2, j);
|
398 | regexp = s.replaceAll("\\d+", Matcher.quoteReplacement("\\d+"));
|
399 | System.out.println("regexp: " + regexp);
|
400 | }
|
401 |
|
402 | public String processIn(Object _in) {
|
403 | String in = (String) _in;
|
404 | if (regexp.length() == 0)
|
405 | return in;
|
406 | else
|
407 | return in.replaceAll("(" + regexp + ")", "[[$1]]");
|
408 | }
|
409 |
|
410 | public void toJava(Code code) {
|
411 | code.line(code.var + " = ((String) " + code.var + ").replaceAll(" + quote("(" + regexp + ")") + ", \"[[$1]]\");");
|
412 | }
|
413 | }
|
414 |
|
415 | static class LFixed extends LearnerImpl {
|
416 | static boolean debug;
|
417 | Object value;
|
418 |
|
419 | public void processInOut(Object in, Object out) {
|
420 | value = out;
|
421 | if (debug)
|
422 | printVars();
|
423 | }
|
424 |
|
425 | public Object processIn(Object in) {
|
426 | return value;
|
427 | }
|
428 |
|
429 | public void toJava(Code code) {
|
430 | code.line(code.var + " = " + quote((String) value) + ";");
|
431 | }
|
432 | }
|
433 |
|
434 | static void fail(String msg) {
|
435 | throw new RuntimeException(msg);
|
436 | }
|
437 |
|
438 | static void assertSameSize(List a, List b) {
|
439 | if (a.size() != b.size())
|
440 | fail("wrong list sizes");
|
441 | }
|
442 |
|
443 | // process lists in parallel
|
444 | // (in and out must be a list of same length)
|
445 | static class LEach extends LearnerImpl {
|
446 | static boolean debug;
|
447 | Learner base;
|
448 |
|
449 | LEach(Learner base) {
|
450 | this.base = base;
|
451 | }
|
452 |
|
453 | public void processInOut(Object _in, Object _out) {
|
454 | List in = (List) _in, out = (List) _out;
|
455 | assertSameSize(in, out);
|
456 | for (int i = 0; i < in.size(); i++)
|
457 | base.processInOut(in.get(i), out.get(i));
|
458 | if (debug)
|
459 | printVars();
|
460 | }
|
461 |
|
462 | public Object processIn(Object _in) {
|
463 | List in = (List) _in;
|
464 | List out = new ArrayList();
|
465 | for (Object x : in)
|
466 | out.add(base.processIn(x));
|
467 | return out;
|
468 | }
|
469 |
|
470 | public void toJava(Code code) {
|
471 | code.line("List out = new ArrayList();");
|
472 | code.line("for (Object x : (List) in) {");
|
473 | code.indent();
|
474 | code.line("in = x;");
|
475 | base.toJava(code);
|
476 | code.line("out.add(in);");
|
477 | code.unindent();
|
478 | code.line("}");
|
479 | code.line("in = out;");
|
480 | }
|
481 | }
|
482 |
|
483 | static class LBoth extends LearnerImpl {
|
484 | ReversibleFunction f;
|
485 | Learner base;
|
486 |
|
487 | LBoth(ReversibleFunction f, Learner base) {
|
488 | this.f = f;
|
489 | this.base = base;
|
490 | }
|
491 |
|
492 | public void processInOut(Object in, Object out) {
|
493 | in = f.process(in);
|
494 | out = f.process(out);
|
495 | base.processInOut(in, out);
|
496 | }
|
497 |
|
498 | public Object processIn(Object in) {
|
499 | in = f.process(in);
|
500 | in = base.processIn(in);
|
501 | in = f.unprocess(in);
|
502 | return in;
|
503 | }
|
504 |
|
505 | public void toJava(Code code) {
|
506 | f.toJava_process(code);
|
507 | base.toJava(code);
|
508 | f.toJava_unprocess(code);
|
509 | }
|
510 | }
|
511 |
|
512 | static class RFJavaTok implements ReversibleFunction {
|
513 | public Object process(Object in) {
|
514 | return JavaTok.split((String) in);
|
515 | }
|
516 |
|
517 | public Object unprocess(Object in) {
|
518 | return JavaTok.join((List) in);
|
519 | }
|
520 |
|
521 | public void toJava_process(Code code) {
|
522 | code.translators("!636", "!class JavaTok");
|
523 | code.line(code.var + " = JavaTok.split((String) " + code.var + ");");
|
524 | }
|
525 |
|
526 | public void toJava_unprocess(Code code) {
|
527 | code.translators("!636", "!class JavaTok");
|
528 | code.line(code.var + " = JavaTok.join((List) " + code.var + ");");
|
529 | }
|
530 | }
|
531 |
|
532 | static class LFixedFunction extends LearnerImpl {
|
533 | Function f;
|
534 |
|
535 | LFixedFunction(Function f) {
|
536 | this.f = f;
|
537 | }
|
538 |
|
539 | public void processInOut(Object in, Object out) {
|
540 | }
|
541 |
|
542 | public Object processIn(Object in) {
|
543 | return f.process(in);
|
544 | }
|
545 |
|
546 | public void toJava(Code code) {
|
547 | f.toJava_process(code);
|
548 | }
|
549 | }
|
550 |
|
551 | static class EscapeCase implements Function {
|
552 | static boolean debug;
|
553 |
|
554 | public Object process(Object _in) {
|
555 | if (debug)
|
556 | System.out.println("EscapeCase: " + _in);
|
557 | String in = (String) _in;
|
558 | return in.equals("case") ? "_case" : in;
|
559 | }
|
560 |
|
561 | public void toJava_process(Code code) {
|
562 | code.line("if (\"case\".equals(" + code.var + ")) " + code.var + " = " + quote("_case") + ";");
|
563 | }
|
564 | }
|
565 | } |