!759 !include #1004027 // WordAdapter static bool adapt_debug; // put func {}'s returning a string in there // not using _log parameter anymore static void deterministicGenerators(L l) { gen(l, "plus1", func { parseLong(gI())+1 }); gen(l, "delta", func { parseLong(g0())*2-parseLong(g1()) }); gen(l, "delta unicode", func { S a = g1(), b = g0(); if (l(a) == 1 && l(b) == 1) ret intToChar(charToInt(b)*2-charToInt(a)); null; }); gen(l, "exponential", func { sqr(parseLong(g0()))/parseLong(g1()) }); gen(l, "assoc", gOneAssoc()); gen(l, "adjective magic", func { lineAbove(findAdjectiveComment(findAdjective())) }); gen(l, "complete line/ll", func { gSearchNeqNoSystem(gLongLog(), func(S s) { startsWithIgnoreCase(s, gI()) }) }); gen(l, "complete line", func { gSearchNeqNoSystem(func(S s) { startsWithIgnoreCase(s, gI()) }) }); gen(l, "longest prefix match -> answer", func { lineBelow(gLongestPrefixMatchNeqIdx(gI())) }); gen(l, "word search", func { gSearchNeqNoSystem(func(S s) { match(gI(), s) }) }); gen(l, "raw string search", func { gSearchNeqNoSystem(func(S s) { containsIgnoreCase(s, gI()) }) }); gen(l, "raw string search (!java)", func { gSearchNeqNoSystem(func(S s) { s.startsWith("!java") && containsIgnoreCase(s, gI()) }) }); gen(l, "yes/no from java", func { L tok = javaTok(gI()); if (contains(tok, "t")) ret "yes"; if (contains(tok, "f")) ret "no"; null; }); gen(l, "learned", func { for (L learn : gAllLearns()) { if (l(learn) >= 2 && match(first(learn), gI())) try answer lastNonSystemLine(learn); } null; }); gen(l, "learned (single step)", func { for (L learn : gAllLearns()) { for (int i = 0; i < l(learn)-1; i++) if (match(learn.get(i), gI())) ret learn.get(i+1); } null; }); gen(l, "learned (single step 2)", func { thislearn: for (L learn : gAllLearns()) { L log = gLog(); int j = l(log)-l(learn)+1; // TODO: wrong logic if (j < 0) null; for (int i = 0; i < l(learn)-1; i++) if (!match(learn.get(i), log.get(j+i))) break thislearn; ret last(learn); // all entries match } null; }); gen(l, "learned (single step 2, with adapter)", func { L log = gLog(); S bestResult = null; double bestValue = 0; thislearn: for (L learn : gAllLearns()) { if (adapt_debug) print("Testing learn: " + struct(learn)); // j is starting point of match in log j: for (int j = max(0, l(log)-l(learn)+1); j < l(log); j++) pcall { if (adapt_debug) print("\nl(log)=" + l(log) + ", j=" + j); Adapter adapter = new WordAdapter; int k; for (k = j; k < l(log); k++) { S in = learn.get(k-j), out = log.get(k); if (adapt_debug) print(in + " => " + out); if (!adapter.canMatch(in, out)) { if (adapt_debug) print("Can't match"); continue j; } adapter = adapter.plus(in, out); if (adapt_debug) printStructure(" ", adapter); } double v = adapter.size(); if (bestResult == null || v < bestValue) { bestResult = adapter.get(learn.get(k-j)); bestValue = v; if (adapt_debug) print("New best: " + formatDouble(bestValue, 1) + " -- " + bestResult); } } } ret bestResult; }); gen(l, "grow suffix", func { S a = g1(), b = g0(); if (b.startsWith(a)) ret b + b.substring(l(a)); null; }); gen(l, "grow prefix", func { S a = g1(), b = g0(); ret growPrefix(a, b); }); gen(l, "strip common prefix + grow prefix", func { S a = g1(), b = g0(); int l = lCommonPrefix(a, b); if (l == 0) null; S s = growPrefix(a.substring(l), b.substring(l)); if (empty(s)) null; ret a.substring(0, l) + s; }); gen(l, "alternate prefix", func { ret commonPrefix(gN(2), gN(4)); }); gen(l, "rewrite", func { gStandardRewrites().get(g0()) }); } static S findAdjective() { ret findAdjective(gI()); } static S findAdjective(S s) { ret findOneOfTheWords(s, "crazy", "nice"); } static int findAdjectiveComment(final S adjective) { if (adjective == null) ret -1; ret gSearchIdx(func(S s) { matchStart("that's", s) && find3(adjective, s) }); }