// universal constants to 1 billion digits download url:
// https://archive.org/details/Math_Constants
//download 1 billion digits of pi here:
//https://stuff.mit.edu/afs/sipb/contrib/pi/pi-billion.txt
//_____________________________________________________________
import java.io.*;
//_____________________________________________________________
public class search
{ 
   String file = getFile2("pi-billion.txt");
   byte[] file2 = getBytes("pi-billion.txt");
   String e = getFile2("euler.txt");
//_____________________________________________________________
public static final void main(String[] args) throws Exception
{
   search bnk = new search();
   bnk.start(args);
}
//_____________________________________________________________
search() throws Exception 
{
}
//___________________________________________________________________________
void start(String[] args)
{
   if (args.length < 7) args = "BRAJYO BRA ALHYM AO HJMYM VAO HARE".split(" ");
   int cnt = 0;
   int[] place = new int[args.length];
   int[] value = new int[args.length];
   String show = "";
   for (int i = 0; i < args.length; i++)
   {
      place[i] = place(args[i]);
      value[i] = value(args[i]);
      show += args[i] + " ";
   }
   System.out.println(show);
   System.out.println("A ----------------------");
 

   String str = "";
   for (int i = 0; i < value.length; i++)
   {
      str += Integer.toString(digits(digits(value[i])));
   }
   String strpss = str;
   int pos = file.indexOf(str);
   String look = file.substring(pos,pos+20);
   String look2 = look.substring(str.length(), str.length() + 5);
   System.out.println(pos + " " + str + " " + look + " " + look2);
   if (look2.equals("75357")) cnt++;
   int tot = value[3-1] + value[5-1] + value[7-1];
   System.out.println(tot);
   if (tot == 777) cnt++;
   pos = file.indexOf(reverse(str));
   String posstr = Integer.toString(pos);
   System.out.println(posstr);
   if (posstr.length() >= 3)
   {
      if (posstr.substring(0,3).equals("735")) cnt++;
   }

   System.out.println("B ----------------------");
   int val13 = value[1-1] + value[3-1];
   int val245 = value[2-1] + value[4-1] + value[5-1];
   if (val13 == 999) cnt++;
   if (val245 == 999) cnt++;
   System.out.println(val13 + " " + val245);
   StringBuffer buf = new StringBuffer();
   buf.append(value[3-1] + " " + value[5-1] + " " + value[7-1]);
   str = buf.toString().replace(" ","");
   pos = file.indexOf(str);
   look = file.substring(pos, pos+20);
   look2 = look.substring(str.length(),str.length() + 5);
   System.out.println(pos + " " + str + " " + look + " " + look2);
   if (look2.equals("29992")) cnt++;

   System.out.println("C --------------------------");
   buf = new StringBuffer();
   buf.append(place[3-1] + " " + place[5-1] + " " + place[7-1]);
   str = buf.toString().replace(" ","");
   pos = file.indexOf(str);
   look = file.substring(pos, pos+20);
   look2 = look.substring(str.length()+1,str.length()+4+1);
   System.out.println(pos + " " + str + " " + look + " " + look2);
   int tot3 = place[2-1] + place[3-1] + place[4-1];
   tot3 += value[2-1] + value[3-1] + value[4-1];
   if (look2.equals("2373")) if (tot3 == 777) cnt++;

   System.out.println("D -----------------------");
   buf = new StringBuffer();
   buf.append(place[1-1] + " " + place[2-1] + " " + place[3-1]);
   str = buf.toString().replace(" ","");
   pos = file.indexOf(str);
   look = file.substring(pos,pos+20);
   look2 = look.substring(str.length(), str.length() + 2);
   String look2b = look.substring(str.length(),str.length() + 11);
   System.out.println(pos + " " + str + " " + look + " " + look2);
   
   boolean ok4 = true;
   if (!look2.equals(Integer.toString(place[7-1]))) ok4 = false;
   if (value[7-1] != 296) ok4 = false;
   
   String last = Integer.toString(place[7-1]);
   String look3 = look2b.substring(last.length(), last.length() + 2);
   String look4 = look2b.substring(last.length() + 2, last.length() + 3);
   String look5 = look2b.substring(last.length() + 3, last.length() + 4);
   String look6 = look2b.substring(last.length() + 4, last.length() + 6);
   int val4 = Integer.parseInt(look3) * Integer.parseInt(look4);
   if (place[5-1] != val4) ok4 = false;
   int dg4 = digits(place[5-1]);
   if (dg4 != Integer.parseInt(look5)) ok4 = false;
   int val4b = place[4-1] + place[6-1];
   if (!reverse(look6).equals(Integer.toString(val4b))) ok4 = false;

   if (ok4) cnt++;

   System.out.println("E ---------------------------");
   int val6 = value[6-1];
   int val7 = value[7-1];
   int valdif = val6 - val7;
   if (valdif == 111) cnt++;
   buf = new StringBuffer();
   buf.append(place[1-1] + " " + place[2-1] + " " + place[3-1] + " " + place[4-1]);
   str = buf.toString().replace(" ","");
   pos = file.indexOf(str);
   look = file.substring(pos,pos+20);
   look2 = look.substring(str.length(), str.length()+3);
   look3 = look.substring(str.length() + 3, str.length() + 5);
   System.out.println(pos + " " + str + " " + look + " " + look2);
   boolean ok5 = true;
   if (!look2.equals("111")) ok5 = false;
   look4 = reverse(Integer.toString(place[1-1]));
   if (!look4.equals(look3)) ok5 = false;
   if (ok5) cnt++;

   System.out.println("F ----------------------------");
   cnt += offset(args);
   System.out.println("G ----------------------------");

   String sj = "";
   int jump = Integer.parseInt(strpss);
   System.out.println(jump);

   for (int i = 0; i < 9 * jump; i += jump)
   {
      sj += file.substring(i,i+1);
   }
   System.out.println(sj);
   String sjr = reverse(sj);
   String sjcomp = Integer.toString(value(args[0])) + Integer.toString(place(args[0]));
   sjcomp += "2153";
   System.out.println(sjcomp);
   if (sjr.equals(sjcomp)) cnt++;


   System.out.println(cnt + " / 18");
   System.out.println("---------------------------");
   if (letter(args)) cnt++;
   if (gen_e(args)) cnt++;
   if (euler(args)) cnt++;
   if (log3(args)) cnt++;
   if (dosqrt2(args)) cnt++;
   if (cnt == 23) System.out.println("WORKS");
   else System.out.println("FAILS");
}
//___________________________________________________________________________
boolean dosqrt2(String[] args)
{
   byte[] bbsqrt2 = getBytes("sqrt2.dec");
   int start = 333;
   if (args.length == 0) args = "BRAJYO BRA ALHYM AO HJMYM VAO HARE".split(" ");
   if (args.length < 6) return(false);
   String s1 = reverse(Integer.toString(value(args[0])));
   s1 += reverse(Integer.toString(place(args[0])));
   String s2 = reverse(Integer.toString(value(args[5])));
   s2 += reverse(Integer.toString(place(args[5])));
   String ans = s2;
   int offset = Integer.parseInt(s1);
   StringBuffer buf = new StringBuffer();
   for (int i = 1; i <= 5; i++)
   {
      int pos = start + i * offset;
      buf.append((char)bbsqrt2[pos]);
   }
   String out = buf.toString();
   System.out.println(start + " " + offset + " " + out);
   if (out.equals(ans)) return(true);
   else return(false);
}
//___________________________________________________________________________
boolean log3(String[] sentence)
{
   if (sentence.length < 7) sentence = "BRAJYO BRA ALHYM AO HJMYM VAO HARE".split(" ");
   if (sentence.length < 6) return(false);
   int val0 = value(sentence[0]);
   int pla0 = place(sentence[0]);
   int val3 = value(sentence[3]);
   int pla3 = place(sentence[3]);
   int val5 = value(sentence[5]);
   int pla5 = place(sentence[5]);
   if (val0 != 913) return(false);
   if (pla0 != 76) return(false);
   if (val3 != 401) return(false);
   if (pla3 != 23) return(false);
   if (val5 != 407) return(false);
   if (pla5 != 29) return(false);
   return(true);
}
//___________________________________________________________________________
boolean euler(String[] sentence)
{
   if (sentence.length < 7) sentence = "BRAJYO BRA ALHYM AO HJMYM VAO HARE".split(" ");
   if (sentence.length < 6) return(false);
   int dig1 = digits(digits(value(sentence[1])));
   int dig2 = digits(digits(value(sentence[2])));
   int dig3 = digits(digits(value(sentence[3])));
   int dig5 = digits(digits(value(sentence[5])));
   String str1 = Integer.toString(dig1);
   String str2 = Integer.toString(dig2);
   String str3 = Integer.toString(dig3);
   String str5 = Integer.toString(dig5);
   String str125 = str1 + str2 + str5;
   String str135 = str1 + str3 + str5;
   String str235 = str2 + str3 + str5;
   int int125 = Integer.parseInt(str125);
   int int135 = Integer.parseInt(str135);
   int int235 = Integer.parseInt(str235);
   if (!checkeuler(1047,int125).equals("3141592653")) return(false);
   if (!checkeuler(1047,int135).equals("3141592653")) return(false);
   if (!checkeuler(1047,int235).equals("3141592653")) return(false);
   return(true);
}
//___________________________________________________________________________
String checkeuler(int start, int offset)
{
  //String e = getFile2("euler.txt");
   String pi = "";
   for (int i = start; i < start + 10 * offset; i += offset)
   {
      pi += e.substring(i,i+1);
   }
System.out.println(pi);
   return(pi);
}

//___________________________________________________________________________
boolean gen_e(String[] sentence)
{
   if (sentence.length < 7) sentence = "BRAJYO BRA ALHYM AO HJMYM VAO HARE".split(" ");
   if (sentence.length < 7) return(false);
   StringBuffer buflook1 = new StringBuffer();
   StringBuffer buflook2 = new StringBuffer();
   buflook1.append(digits(digits(value(sentence[4]))));
   buflook1.append(digits(digits(value(sentence[5]))));
   buflook1.append(digits(digits(value(sentence[6]))));
   buflook2.append(sentence[4].length());
   buflook2.append(sentence[5].length());
   buflook2.append(sentence[6].length());
   String look1 = buflook1.toString(); 
   String look2 = buflook2.toString();
   int pos1 = e.indexOf(look1);
   int pos2 = e.indexOf(look2);
   System.out.println(look1 + " " + pos1);
   System.out.println(look2 + " " + pos2);
   if (pos1 > 4) return(false);
   if (pos2 != 3736) return(false); 
   return(true);
}
//___________________________________________________________________________
boolean letter(String[] args)
{
   
   String pi = "3141592653589793";
   String look = "31415926";
   String gen = "";
   for (int i = 0; i < args.length; i++)
   {
      gen += args[i];
   }
   String tot = Integer.toString(value(gen));
   if (tot.length() != 4) return(false);
   System.out.println(tot);
   for (int i = 0; i < 200000; i++)
   {
      int total3 = 0;
      StringBuffer buf3 = new StringBuffer();
      for (int j = 0; j < gen.length(); j++)
      {
         String s = gen.substring(j,j+1);
         int val = value(s);
         int pla = place(s);
         total3 += pla;
         if (total3+1 >= file2.length) continue;
         buf3.append((char)file2[total3+i]);
      }
      if (buf3.toString().substring(0,look.length()).equals(look)) 
      {
         String rev = reverse(Integer.toString(i));
         System.out.println(i);
         System.out.println(rev);
         System.out.println(buf3.toString()); 
         if (rev.length() != 6) return(false);
         if (!rev.substring(2,4).equals("53")) return(false);
         if (!rev.substring(1,2).equals(tot.substring(1,2))) return(false);
         if (!rev.substring(4,6).equals(tot.substring(2,4))) return(false);
         String one = tot.substring(0,1);
         int valone = Integer.parseInt(one);
         int cube = valone * valone * valone;
         if (!rev.substring(0,1).equals(Integer.toString(cube))) return(false);
         return(true);
      }
   }
   return(false);
}
//___________________________________________________________________________
String reverse(String str)
{
   String out = "";
   for (int i = str.length() - 1; i >= 0; i--)
   {
      String s = str.substring(i,i+1);
      out += s;
   }
   return(out);
}
//___________________________________________________________________________
int digits(int val)
{
   String str = Integer.toString(val);
   int total = 0;
   for (int i = 0; i < str.length(); i++)
   {
      String s = str.substring(i,i+1);
      total += Integer.parseInt(s);
   }
   return(total);
}
//___________________________________________________________________________
String getFile2(String file) 
{
   StringBuffer buf=new StringBuffer();
   String str;
  try
  {
   BufferedReader in = new BufferedReader (new FileReader (file));
   while((str=in.readLine())!=null)
   {
      buf.append(str);
   }
   in.close();
  }
  catch (Exception e)
  {
   System.out.println(e);
  }
   return (buf.toString());
}
//__________________________________________________
int value(String str)
{
   int total = 0;
   for (int i = 0; i < str.length(); i++)
   {
      int v = 0;
      String st2 = str.substring(i,i+1);
      byte[] st3 = st2.getBytes();
      byte st = st3[0];
      if (st == 'a') v = 1;
      else if (st == 'b') v = 2;
      else if (st == 'c') v = 3;
      else if (st == 'd') v = 4;
      else if (st == 'e') v = 5;
      else if (st == 'f') v = 6;
      else if (st == 'g') v = 7;
      else if (st == 'h') v = 8;
      else if (st == 'i') v = 9;
      else if (st == 'j') v = 10;
      else if (st == 'k') v = 20;
      else if (st == 'l') v = 30;    
      else if (st == 'm') v = 40;
      else if (st == 'n') v = 50;
      else if (st == 'o') v = 60;
      else if (st == 'p') v = 70;
      else if (st == 'q') v = 80;
      else if (st == 'r') v = 90;
      else if (st == 's') v = 100;
      else if (st == 't') v = 200;
      else if (st == 'u') v = 300;
      else if (st == 'v') v = 400;
      else if (st == 'w') v = 500;
      else if (st == 'x') v = 600;
      else if (st == 'y') v = 700;
      else if (st == 'z') v = 800;
      else if (st == 'A') v = 1;
      else if (st == 'B') v = 2;
      else if (st == 'G') v = 3;
      else if (st == 'D') v = 4;
      else if (st == 'H') v = 5;
      else if (st == 'V') v = 6;
      else if (st == 'Z') v = 7;
      else if (st == 'C') v = 8;
      else if (st == 'T') v = 9;
      else if (st == 'Y') v = 10;
      else if (st == 'K') v = 20;
      else if (st == 'L') v = 30;
      else if (st == 'M') v = 40;
      else if (st == 'N') v = 50;
      else if (st == 'S') v = 60;
      else if (st == 'I') v = 70;
      else if (st == 'F') v = 80;
      else if (st == 'E') v = 90;
      else if (st == 'Q') v = 100;
      else if (st == 'R') v = 200;
      else if (st == 'J') v = 300;
      else if (st == 'O') v = 400;
      total += v;
   }
   return(total);
}
//__________________________________________________
int place(String str)
{
   int total = 0;
   for (int i = 0; i < str.length(); i++)
   {
      int v = 0;
      String st2 = str.substring(i,i+1);
      byte[] st3 = st2.getBytes();
      byte st = st3[0];
      if (st == 'a') v = 1;
      else if (st == 'b') v = 2;
      else if (st == 'c') v = 3;
      else if (st == 'd') v = 4;
      else if (st == 'e') v = 5;
      else if (st == 'f') v = 6;
      else if (st == 'g') v = 7;
      else if (st == 'h') v = 8;
      else if (st == 'i') v = 9;
      else if (st == 'j') v = 10;
      else if (st == 'k') v = 11;
      else if (st == 'l') v = 12;    
      else if (st == 'm') v = 13;
      else if (st == 'n') v = 14;
      else if (st == 'o') v = 15;
      else if (st == 'p') v = 16;
      else if (st == 'q') v = 17;
      else if (st == 'r') v = 18;
      else if (st == 's') v = 19;
      else if (st == 't') v = 20;
      else if (st == 'u') v = 21;
      else if (st == 'v') v = 22;
      else if (st == 'w') v = 23;
      else if (st == 'x') v = 24;
      else if (st == 'y') v = 25;
      else if (st == 'z') v = 26;
      else if (st == 'A') v = 1;
      else if (st == 'B') v = 2;
      else if (st == 'G') v = 3;
      else if (st == 'D') v = 4;
      else if (st == 'H') v = 5;
      else if (st == 'V') v = 6;
      else if (st == 'Z') v = 7;
      else if (st == 'C') v = 8;
      else if (st == 'T') v = 9;
      else if (st == 'Y') v = 10;
      else if (st == 'K') v = 11;
      else if (st == 'L') v = 12;
      else if (st == 'M') v = 13;
      else if (st == 'N') v = 14;
      else if (st == 'S') v = 15;
      else if (st == 'I') v = 16;
      else if (st == 'F') v = 17;
      else if (st == 'E') v = 18;
      else if (st == 'Q') v = 19;
      else if (st == 'R') v = 20;
      else if (st == 'J') v = 21;
      else if (st == 'O') v = 22;
      total += v;
   }
   return(total);
}
//__________________________________________________
int offset(String[] args)
{
   int cnt = 0;
   if (startit(970990704,1,1,1,args)) cnt++;
   if (startit(351483,1,2,1,args)) cnt++;
   if (startit(16244873,2,1,1,args)) cnt++;
   if (startit(491764,2,2,1,args)) cnt++;

   if (startit(4226340,1,1,2,args)) cnt++;
   if (startit(21277083,1,2,2,args)) cnt++;
   if (startit(2478918,2,1,2,args)) cnt++;
   return(cnt);
}
//___________________________________________________________________________
boolean startit(int offset, int type1, int type2, int type3, String[] args3)
{
   int[] args = new int[args3.length]; // {913,203,86,401,395,407,296};
   int[] args2 = new int[args3.length]; // {76,23,41,23,62,29,44};
   String argsn = ""; // "4555828";
   for (int i = 0; i < args3.length; i++)
   {
      args[i] = value(args3[i]);
      args2[i] = place(args3[i]);
      int dg = digits(args[i]);
      int dg2 = digits(dg);
      if (type3 == 1) argsn += Integer.toString(dg2);
      if (type3 == 2) argsn += Integer.toString(args3[i].length());
   }
   int pos = 0;
   String out = "";
   int val = 0;
   int j = offset - 1;
   out = "";
   pos = 0;
   StringBuffer buf = new StringBuffer();
   for (int i = 0; i < args.length; i++)
   {
      if (type1 == 1) val = args[i]; else val = args2[i];
      if (type2 == 1) pos += val; else pos = val;
      buf.append((char)file2[pos + j]);     
   }
   if (buf.toString().equals(argsn)) return(true);
   return(false);
}
//___________________________________________________________________________
byte[] getBytes(String name) 
{
   byte[] b = new byte[0];
  try
  {
   File file = new File(name);
   b = new byte[(int) file.length()];
   FileInputStream fileInputStream = new FileInputStream(file);
   fileInputStream.read(b);
  }
  catch (Exception e)
  {
   System.out.println(e);
  }
   return(b);
}
}//__________________________________________________________________________
