!636 !quickmain !auto-import !standard functions interface Learner { void processInOut(String in, String out); String processIn(String in); } // only "learns" the "id" function class LId implements Learner { public void processInOut(String in, String out) { } public String processIn(String in) { return in; } } main { static List fullExamples = new ArrayList(); static List halfExamples = new ArrayList(); static List examples1, examples2; psvm { parse(args.length != 0 ? args[0] : null); calculate(); } static void parse(String arg) tex { String text; if (arg != null) text = loadSnippet(arg); else { text = loadTextFile("input/input.txt", null); if (text == null) text = loadSnippet("#2000377"); } System.out.println(text); String in = null, out = null; for (String line : toLines(text)) { if (line.startsWith("I")) { // "In: " or "I: " if (in != null) halfExamples.add(in); in = unquote(line.substring(line.indexOf(':')+1).trim()); out = null; } else if (line.startsWith("O")) { // "Out: " or "O: " out = unquote(line.substring(line.indexOf(':')+1).trim()); System.out.println(quote(in) + " => " + quote(out)); fullExamples.add(new String[] {in, out}); in = out = null; } } if (in != null) halfExamples.add(in); } static void calculate() tex { if (fullExamples.size() < 2) throw new RuntimeException("Too few examples (" + fullExamples.size() + ")"); int splitPoint = fullExamples.size()-1; System.out.println("Full examples: " + fullExamples.size() + ", splitPoint: " + splitPoint); examples1 = fullExamples.subList(0, splitPoint); examples2 = fullExamples.subList(splitPoint, fullExamples.size()); Learner learner = findOKLearner(); if (learner == null) print "\nProblem not solved" else { print "\nSolved!\n" for (String in : halfExamples) { String out = learner.processIn(in); System.out.println(quote(in) + " =>! " + quote(out)); } } } static Learner findOKLearner() { for (Learner learner : makeLearners()) try { if (learnerOK(learner)) return learner; } catch (Throwable e) { e.printStackTrace(); } return null; } static boolean learnerOK(Learner learner) { for (String[] e : examples1) { learner.processInOut(e[0], e[1]); } for (String[] e : fullExamples) { String out = learner.processIn(e[0]); if (!e[1].equals(out)) { System.out.println("[fail] " + learner + " on " + quote(e[0]) + " - " + quote(out) + " vs " + quote(e[1])); return false; } } return true; // all test examples passed } static Iterable makeLearners() { List list = new ArrayList(); list.add(new LId()); list.add(new LPrefixSuffix()); return list; } public static String unquote(String s) { if (s.startsWith("\"") && s.endsWith("\"") && s.length() > 1) return s.substring(1, s.length()-1).replace("\\\"", "\"").replace("\\\\", "\\"); // SHOULD work... else return s; // Return SOMETHING } public static String quote(String s) { if (s == null) return "null"; return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"") + "\""; } // learns to exchange common prefixes and suffixes static class LPrefixSuffix implements Learner { String prefixIn, suffixIn, prefixOut, suffixOut; public void processInOut(String in, String out) { updateIn(in); prefixOut = prefixOut == null ? out : commonPrefix(prefixOut, out); suffixOut = suffixOut == null ? out : commonSuffix(suffixOut, out); } void updateIn(String in) { prefixIn = prefixIn == null ? in : commonPrefix(prefixIn, in); suffixIn = suffixIn == null ? in : commonSuffix(suffixIn, in); } public String processIn(String in) { //System.out.println("[before last info] " + quote(prefixIn) + " " + quote(suffixIn) + " " + quote(prefixOut) + " " + quote(suffixOut)); //System.out.println("[last info] " + quote(in)); // use latest information String p = prefixIn, s = suffixIn; updateIn(in); prefixOut = prefixOut.substring(0, prefixOut.length()-(p.length()-prefixIn.length())); suffixOut = suffixOut.substring(s.length()-suffixIn.length()); //System.out.println("[after last info] " + quote(prefixIn) + " " + quote(suffixIn) + " " + quote(prefixOut) + " " + quote(suffixOut)); String core = in.substring(prefixIn.length(), in.length()-suffixIn.length()); return prefixOut + core + suffixOut; } } }