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

120
LINES

< > BotCompany Repo | #1002049 // Conversion Bot (v1)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (1487L/10K/33K).

1  
!752
2  
3  
// Numeric quadruple
4  
static class NQ {
5  
  BigInteger a;
6  
  S b;
7  
  BigInteger c;
8  
  S d;
9  
  
10  
  *() {}
11  
  *(S a, S *b, S c, S *d) {
12  
    this.a = smartBigint(a);
13  
    this.c = smartBigint(c);
14  
  }
15  
  *(BigInteger *a, S *b, BigInteger *c, S *d) {}
16  
  
17  
  public boolean equals(O o) {
18  
    ret stdEq(this, o, "a", "b", "c", "d");
19  
  }
20  
  
21  
  public S toString() {
22  
    ret a + " " + b + " is " + c + " " + d;
23  
  }
24  
  
25  
  NQ reverse() {
26  
    ret new NQ(c, d, a, b);
27  
  }
28  
29  
  bool valid() {
30  
    ret neq(a, 0) && neq(c, 0);
31  
  }
32  
}
33  
34  
static new L<NQ> terms;
35  
36  
p {
37  
  load("terms");
38  
}
39  
40  
synchronized answer {
41  
  if (match("* * is * *", s, m) || match("* * are * *", s, m)) {
42  
    NQ nq = new NQ(m.unq(0), simplify(m.unq(1)), m.unq(2), simplify(m.unq(3)));
43  
    if (terms.contains(nq))
44  
      ret "I know!";
45  
    terms.add(nq);
46  
    save("terms");
47  
    ret "OK, added to theory. Now " + l(terms) + " entries";
48  
  }
49  
  
50  
  if (match("show theory", s)) {
51  
    ret structure(allToString(terms));
52  
  }
53  
  
54  
  if (match("how many * * * *", s, m)) {
55  
    S isAre = m.get(1);
56  
    if (litlist("is", "are").contains(toLower(isAre))) {
57  
      BigInteger a = bigint(m.unq(2));
58  
      S b = m.unq(3);
59  
      S d = m.unq(0);
60  
      Rat c = calc(new Rat(a), b, d);
61  
      if (c == null)
62  
        ret "I don't know";
63  
      ret a + " " + b + " " + isAre + " " + c.mixed() + " " + d + "!";
64  
    }
65  
  }
66  
}
67  
68  
/*static Rat calc(Rat a, S b, S d) {
69  
  b = simplify(b);
70  
  d = simplify(d);
71  
  for (NQ nq : terms) {
72  
    if (eq(nq.b, b) && eq(nq.d, d))
73  
      ret calcForward(nq, a);
74  
    if (eq(nq.d, b) && eq(nq.b, d))
75  
      ret calcForward(nq.reverse(), a);
76  
  }
77  
  null;
78  
}*/
79  
80  
static Rat calc(Rat a, S b, S d) {
81  
  b = simplify(b);
82  
  d = simplify(d);
83  
  ret calcTransitive(a, b, d, new TreeSet);
84  
}
85  
86  
// b is the unit we have, d is the unit we're searching for
87  
static Rat calcTransitive(Rat a, S b, S d, Set<S> seen) {
88  
  print(format("calcTransitive * * * *", a, b, d, structure(seen)));
89  
  if (eq(b, d)) ret a; // done
90  
  if (seen.contains(b)) ret null;
91  
  seen.add(b);
92  
  for (NQ nq : terms) if (nq.valid()) {
93  
    twice {
94  
      if (eq(nq.b, b)) {
95  
        Rat r = calcTransitive(calcForward(nq, a), nq.d, d, seen);
96  
        if (r != null) ret r;
97  
      }
98  
      nq = nq.reverse();
99  
    }
100  
  }
101  
  null;
102  
}
103  
104  
static Rat calcForward(NQ nq, Rat a) {
105  
  ret a.multiply(nq.c).divide(nq.a);
106  
}
107  
108  
static S simplify(S s) {
109  
  ret toSingular(toLower(s));
110  
}
111  
112  
static S toSingular(S s) {
113  
  ret dropSuffix("s", s); // yeah it's rough
114  
}
115  
116  
static BigInteger smartBigint(S s) {
117  
  if (litlist("a", "an", "one").contains(toLower(s)))
118  
    ret bigint(1);
119  
  ret bigint(s);
120  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1002049
Snippet name: Conversion Bot (v1)
Eternal ID of this version: #1002049/1
Text MD5: 1f64de1db27e5056aa5c8aecb8a6eadb
Transpilation MD5: e80cb1000e7d0f661c9ecff50851c6b6
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-12-16 18:20:48
Source code size: 2666 bytes / 120 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 658 / 968
Referenced in: [show references]