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

229
LINES

< > BotCompany Repo | #678 // IOIOI Processor (v3, with input pattern finding)

JavaX source code - run with: x30.jar

1  
!636
2  
!quickmain
3  
!auto-import
4  
!standard functions
5  
6  
interface Learner {
7  
  public void processInOut(String in, String out);
8  
  public String processIn(String in);
9  
  public void toJava(Code code);
10  
}
11  
12  
class Code {
13  
  StringBuilder buf = new StringBuilder();
14  
  
15  
  void line(String line) {
16  
    buf.append(line).append('\n');
17  
  }
18  
}
19  
20  
main {
21  
  static List<String[]> fullExamples = new ArrayList<String[]>();
22  
  static List<String> halfExamples = new ArrayList<String>();
23  
  static List<String[]> examples1, examples2;
24  
  
25  
  psvm {
26  
    parse(args.length != 0 ? args[0] : null);
27  
    calculate();
28  
  }
29  
  
30  
  static void parse(String arg) tex {
31  
    String text;
32  
    if (arg != null)
33  
      text = loadSnippet(arg);
34  
    else {
35  
      text = loadTextFile("input/input.txt", null);
36  
      if (text == null) text = loadSnippet("#2000455"); // example input
37  
    }
38  
      
39  
    System.out.println(text);
40  
    String in = null, out = null;
41  
    
42  
    for (String line : toLines(text)) {
43  
      if (line.startsWith("I")) { // "In: " or "I: "
44  
        if (in != null)
45  
          halfExamples.add(in);
46  
        in = unquote(line.substring(line.indexOf(':')+1).trim());
47  
        out = null;
48  
      } else if (line.startsWith("O")) { // "Out: " or "O: "
49  
        out = unquote(line.substring(line.indexOf(':')+1).trim());
50  
        System.out.println(quote(in) + " => " + quote(out));
51  
        fullExamples.add(new String[] {in, out});
52  
        in = out = null;
53  
      }
54  
    }
55  
    
56  
    if (in != null)
57  
      halfExamples.add(in);
58  
  }
59  
  
60  
  static void calculate() tex {
61  
    if (fullExamples.size() < 2)
62  
      throw new RuntimeException("Too few examples (" + fullExamples.size() + ")");
63  
    int splitPoint = fullExamples.size()-1;
64  
    System.out.println("Full examples: " + fullExamples.size() + ", splitPoint: " + splitPoint);
65  
    examples1 = fullExamples.subList(0, splitPoint);
66  
    examples2 = fullExamples.subList(splitPoint, fullExamples.size());
67  
    
68  
    Learner learner = findOKLearner();
69  
    if (learner == null)
70  
      print "\nProblem not solved"
71  
    else {
72  
      print "\nSolved!\n"
73  
      Code code = new Code();
74  
      learner.toJava(code);
75  
      System.out.println(code.buf);
76  
      for (String in : halfExamples) {
77  
        String out = learner.processIn(in);
78  
        System.out.println(quote(in) + " =>! " + quote(out));
79  
      }
80  
    }
81  
  }
82  
  
83  
  static Learner findOKLearner() {
84  
    for (Learner learner : makeLearners()) try {
85  
      if (learnerOK(learner))
86  
        return learner;
87  
    } catch (Throwable e) {
88  
      e.printStackTrace();
89  
    }
90  
    return null;
91  
  }
92  
  
93  
  static boolean learnerOK(Learner learner) {
94  
    for (String[] e : examples1) {
95  
      learner.processInOut(e[0], e[1]);
96  
    }
97  
    for (String[] e : fullExamples) {
98  
      String out = learner.processIn(e[0]);
99  
      if (!e[1].equals(out)) {
100  
        System.out.println("[fail] " + learner + " on " + quote(e[0]) + " - " + quote(out) + " vs " + quote(e[1]));
101  
        return false;
102  
      }
103  
    }
104  
    return true; // all test examples passed
105  
  }
106  
  
107  
  static Iterable<Learner> makeLearners() {
108  
    List<Learner> list = new ArrayList<Learner>();
109  
    //list.add(new LId()); // subsumed by trivial case of PrefixSuffix
110  
    list.add(new LPrefixSuffix());
111  
    list.add(new LSplitInput(new LOutPattern()));
112  
    list.add(new LInputPattern());
113  
    return list;
114  
  }
115  
  
116  
  public static String unquote(String s) {
117  
    if (s.startsWith("\"") && s.endsWith("\"") && s.length() > 1)
118  
      return s.substring(1, s.length()-1).replace("\\\"", "\"").replace("\\\\", "\\"); // SHOULD work...
119  
    else
120  
      return s; // Return SOMETHING
121  
  }
122  
  
123  
  public static String quote(String s) {
124  
    if (s == null) return "null";
125  
    return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
126  
  }
127  
  
128  
  // splits the input at some point, takes only one part
129  
  static class LSplitInput implements Learner {
130  
    int splitIdx = 1; // split after first character
131  
    Learner baseLearner;
132  
    
133  
    LSplitInput(Learner baseLearner) {
134  
      this.baseLearner = baseLearner;
135  
    }
136  
    
137  
    public void processInOut(String in, String out) {
138  
      in = in.substring(splitIdx);
139  
      baseLearner.processInOut(in, out);
140  
    }
141  
    
142  
    public String processIn(String in) {
143  
      in = in.substring(splitIdx);
144  
      return baseLearner.processIn(in);
145  
    }
146  
    
147  
    public void toJava(Code code) {
148  
      code.line("in = in.substring(" + splitIdx + ");");
149  
      baseLearner.toJava(code);
150  
    }
151  
  }
152  
  
153  
  // if input appears in output in fixed pattern
154  
  static class LOutPattern implements Learner {
155  
    String pattern = "%!%";
156  
157  
    public void processInOut(String in, String out) {
158  
      pattern = out.replace(in, "%!%");
159  
    }
160  
    
161  
    public String processIn(String in) {
162  
      return pattern.replace("%!%", in);
163  
    }
164  
    
165  
    public void toJava(Code code) {
166  
      code.line("in = " + quote(pattern) + ".replace(" + quote("%!%") + ", in);");
167  
    }
168  
  }
169  
  
170  
  // learns to exchange common prefixes and suffixes
171  
  static class LPrefixSuffix implements Learner {
172  
    String prefixIn, suffixIn, prefixOut, suffixOut;
173  
    
174  
    public void processInOut(String in, String out) {
175  
      updateIn(in);
176  
      prefixOut = prefixOut == null ? out : commonPrefix(prefixOut, out);
177  
      suffixOut = suffixOut == null ? out : commonSuffix(suffixOut, out);
178  
    }
179  
    
180  
    void updateIn(String in) {
181  
      prefixIn = prefixIn == null ? in : commonPrefix(prefixIn, in);
182  
      suffixIn = suffixIn == null ? in : commonSuffix(suffixIn, in);
183  
    }
184  
185  
    public String processIn(String in) {
186  
      //System.out.println("[before last info] " + quote(prefixIn) + " " + quote(suffixIn) + " " + quote(prefixOut) + " " + quote(suffixOut));
187  
      //System.out.println("[last info] " + quote(in));
188  
      
189  
      // use latest information
190  
      String p = prefixIn, s = suffixIn;
191  
      updateIn(in);
192  
      prefixOut = prefixOut.substring(0, prefixOut.length()-(p.length()-prefixIn.length()));
193  
      suffixOut = suffixOut.substring(s.length()-suffixIn.length());
194  
      
195  
      //System.out.println("[after last info] " + quote(prefixIn) + " " + quote(suffixIn) + " " + quote(prefixOut) + " " + quote(suffixOut));
196  
      String core = in.substring(prefixIn.length(), in.length()-suffixIn.length());
197  
      return prefixOut + core + suffixOut;
198  
    }
199  
    
200  
    public void toJava(Code code) {
201  
      throw new RuntimeException("TODO");
202  
    }
203  
  }
204  
  
205  
  // for "find" tasks (e.g. "abcde" to "[[abc]]de")
206  
  static class LInputPattern implements Learner {
207  
    String regexp = "";
208  
    
209  
    public void processInOut(String in, String out) {
210  
      int i = out.indexOf("[["), j = out.indexOf("]]", i+1);
211  
      if (j < 0) return;
212  
      String s = out.substring(i+2, j);
213  
      regexp = s.replaceAll("\\d+", Matcher.quoteReplacement("\\d+"));
214  
      System.out.println("regexp: " + regexp);
215  
    }
216  
    
217  
    public String processIn(String in) {
218  
      if (regexp.length() == 0)
219  
        return in;
220  
      else
221  
        return in.replaceAll("(" + regexp + ")", "[[$1]]");
222  
    }
223  
    
224  
    public void toJava(Code code) {
225  
      code.line("in = in.replaceAll(" + quote("(" + regexp + ")") + ", \"[[$1]]\");");
226  
    }
227  
  }
228  
  
229  
}

Author comment

Began life as a copy of #677

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #678
Snippet name: IOIOI Processor (v3, with input pattern finding)
Eternal ID of this version: #678/1
Text MD5: 16b65b390a79b14054c2a8f9f136b1a4
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-07-24 20:55:51
Source code size: 7263 bytes / 229 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 655 / 539
Referenced in: [show references]