1 | !636 |
2 | !quickmain |
3 | !auto-import |
4 | !standard functions |
5 | |
6 | interface Learner {
|
7 | void processInOut(String in, String out); |
8 | String processIn(String in); |
9 | } |
10 | |
11 | // only "learns" the "id" function |
12 | class LId implements Learner {
|
13 | public void processInOut(String in, String out) {
|
14 | } |
15 | |
16 | public String processIn(String in) {
|
17 | return in; |
18 | } |
19 | } |
20 | |
21 | main {
|
22 | static List<String[]> fullExamples = new ArrayList<String[]>(); |
23 | static List<String> halfExamples = new ArrayList<String>(); |
24 | static List<String[]> examples1, examples2; |
25 | |
26 | psvm {
|
27 | parse(args.length != 0 ? args[0] : null); |
28 | calculate(); |
29 | } |
30 | |
31 | static void parse(String arg) tex {
|
32 | String text; |
33 | if (arg != null) |
34 | text = loadSnippet(arg); |
35 | else {
|
36 | text = loadTextFile("input/input.txt", null);
|
37 | if (text == null) text = loadSnippet("#2000377");
|
38 | } |
39 | |
40 | System.out.println(text); |
41 | String in = null, out = null; |
42 | |
43 | for (String line : toLines(text)) {
|
44 | if (line.startsWith("I")) { // "In: " or "I: "
|
45 | if (in != null) |
46 | halfExamples.add(in); |
47 | in = unquote(line.substring(line.indexOf(':')+1).trim());
|
48 | out = null; |
49 | } else if (line.startsWith("O")) { // "Out: " or "O: "
|
50 | out = unquote(line.substring(line.indexOf(':')+1).trim());
|
51 | System.out.println(quote(in) + " => " + quote(out)); |
52 | fullExamples.add(new String[] {in, out});
|
53 | in = out = null; |
54 | } |
55 | } |
56 | |
57 | if (in != null) |
58 | halfExamples.add(in); |
59 | } |
60 | |
61 | static void calculate() tex {
|
62 | if (fullExamples.size() < 2) |
63 | throw new RuntimeException("Too few examples (" + fullExamples.size() + ")");
|
64 | int splitPoint = fullExamples.size()-1; |
65 | System.out.println("Full examples: " + fullExamples.size() + ", splitPoint: " + splitPoint);
|
66 | examples1 = fullExamples.subList(0, splitPoint); |
67 | examples2 = fullExamples.subList(splitPoint, fullExamples.size()); |
68 | |
69 | Learner learner = findOKLearner(); |
70 | if (learner == null) |
71 | print "\nProblem not solved" |
72 | else {
|
73 | print "\nSolved!\n" |
74 | for (String in : halfExamples) {
|
75 | String out = learner.processIn(in); |
76 | System.out.println(quote(in) + " =>! " + quote(out)); |
77 | } |
78 | } |
79 | } |
80 | |
81 | static Learner findOKLearner() {
|
82 | for (Learner learner : makeLearners()) try {
|
83 | if (learnerOK(learner)) |
84 | return learner; |
85 | } catch (Throwable e) {
|
86 | e.printStackTrace(); |
87 | } |
88 | return null; |
89 | } |
90 | |
91 | static boolean learnerOK(Learner learner) {
|
92 | for (String[] e : examples1) {
|
93 | learner.processInOut(e[0], e[1]); |
94 | } |
95 | for (String[] e : fullExamples) {
|
96 | String out = learner.processIn(e[0]); |
97 | if (!e[1].equals(out)) {
|
98 | System.out.println("[fail] " + learner + " on " + quote(e[0]) + " - " + quote(out) + " vs " + quote(e[1]));
|
99 | return false; |
100 | } |
101 | } |
102 | return true; // all test examples passed |
103 | } |
104 | |
105 | static Iterable<Learner> makeLearners() {
|
106 | List<Learner> list = new ArrayList<Learner>(); |
107 | list.add(new LId()); |
108 | list.add(new LPrefixSuffix()); |
109 | return list; |
110 | } |
111 | |
112 | public static String unquote(String s) {
|
113 | if (s.startsWith("\"") && s.endsWith("\"") && s.length() > 1)
|
114 | return s.substring(1, s.length()-1).replace("\\\"", "\"").replace("\\\\", "\\"); // SHOULD work...
|
115 | else |
116 | return s; // Return SOMETHING |
117 | } |
118 | |
119 | public static String quote(String s) {
|
120 | if (s == null) return "null"; |
121 | return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
|
122 | } |
123 | |
124 | // learns to exchange common prefixes and suffixes |
125 | static class LPrefixSuffix implements Learner {
|
126 | String prefixIn, suffixIn, prefixOut, suffixOut; |
127 | |
128 | public void processInOut(String in, String out) {
|
129 | updateIn(in); |
130 | prefixOut = prefixOut == null ? out : commonPrefix(prefixOut, out); |
131 | suffixOut = suffixOut == null ? out : commonSuffix(suffixOut, out); |
132 | } |
133 | |
134 | void updateIn(String in) {
|
135 | prefixIn = prefixIn == null ? in : commonPrefix(prefixIn, in); |
136 | suffixIn = suffixIn == null ? in : commonSuffix(suffixIn, in); |
137 | } |
138 | |
139 | public String processIn(String in) {
|
140 | //System.out.println("[before last info] " + quote(prefixIn) + " " + quote(suffixIn) + " " + quote(prefixOut) + " " + quote(suffixOut));
|
141 | //System.out.println("[last info] " + quote(in));
|
142 | |
143 | // use latest information |
144 | String p = prefixIn, s = suffixIn; |
145 | updateIn(in); |
146 | prefixOut = prefixOut.substring(0, prefixOut.length()-(p.length()-prefixIn.length())); |
147 | suffixOut = suffixOut.substring(s.length()-suffixIn.length()); |
148 | |
149 | //System.out.println("[after last info] " + quote(prefixIn) + " " + quote(suffixIn) + " " + quote(prefixOut) + " " + quote(suffixOut));
|
150 | String core = in.substring(prefixIn.length(), in.length()-suffixIn.length()); |
151 | return prefixOut + core + suffixOut; |
152 | } |
153 | } |
154 | |
155 | } |
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: | #639 |
| Snippet name: | IOIOI Processor (v1, does prefixes and suffixes) |
| Eternal ID of this version: | #639/1 |
| Text MD5: | 721155e40b6f047d3c353012a45c101f |
| Author: | stefan |
| Category: | |
| Type: | JavaX source code |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2015-07-24 20:02:58 |
| Source code size: | 4983 bytes / 155 lines |
| Pitched / IR pitched: | No / Yes |
| Views / Downloads: | 1175 / 1026 |
| Referenced in: | [show references] |