// ACM South-Pacific Programming Contest checking program Problem K 2011
//
// 1. java verifyK teamoutput judgeoutput | more
//
// 2. Checkbox "Use External Validator" then enter in PC^2 the following
//    for each problem that you want to use the checker.
//    a) Validator Program: verifyK.class  
//    b) Validator Command Line: java verifyK {:outfile} {:ansfile} {:resfile}
//
import java.io.*;
import java.util.*;


public class verifyK
{
 static int teamValue, judgeValue, targetValue;

 public static void main(String argv[]) 
 {
   BufferedReader input1, input2;   // temporary buffers
   input1 = input2 = new BufferedReader(new InputStreamReader(System.in));
   String file1 = new String("junk"); //new String("estdout.pc2");
   String file2 = new String("junk"); //new String("problemA_out.txt");
   PrintWriter out= new PrintWriter(new OutputStreamWriter(System.out));
   PrintWriter result= new PrintWriter(new OutputStreamWriter(System.out));

   try {

   if ( !(argv.length == 3 || argv.length == 2 || argv.length == 0) )
   {
     System.err.println("Usage: java verifyE teamoutput judgeoutput [voutput]");
     System.exit(1);
   }
 
   if (argv.length > 0) file1 = argv[0];
   if (argv.length > 1) file2 = argv[1];

   input1 = new BufferedReader(new FileReader(file1));
   input2 = new BufferedReader(new FileReader(file2));
   
   if (argv.length > 2) {
       result = new PrintWriter(new FileWriter(argv[2]));
       result.write( "<?xml version=\"1.0\"?>\n" );
       out = new PrintWriter(new FileWriter("verifyK.log"));
   }

   } catch (Exception e) 
   { 
       System.err.println("Error opening files "+file1+" and "+file2); 
       System.exit(1); 
   }

   try {
   ArrayList<String> lineList1, lineList2; 

   lineList1 = new ArrayList<String>();
   try{ 
     while (input1.ready())
     {
        String line = input1.readLine();
        int m = line.length()-1;
        while (m>=0 && (line.charAt(m)==' ' || line.charAt(m)=='\t') ) m--;
        if (m>-1) lineList1.add(line.substring(0,m+1));
        else      lineList1.add(new String()); // empty line
     }
   } catch (Exception e) 
   { System.err.println("Error reading file "+file1); System.exit(1); }

   int l1 = lineList1.size()-1;  // strip end blank lines
   while (l1>=0 && ((String) lineList1.get(l1)).length()==0 ) 
   { lineList1.remove(l1--); }

   lineList2 = new ArrayList<String>();
   try {
     String line; 
     while (input2.ready())
     {
        line = input2.readLine();
        int m = line.length()-1;
        while (m>=0 && (line.charAt(m)==' ' || line.charAt(m)=='\t') ) m--;
        if (m>-1) lineList2.add(line.substring(0,m+1));
        else      lineList2.add(new String()); // empty line
     }
   } catch (Exception e) 
   { System.err.println("Error reading file "+file2); System.exit(1); }

   int l2 = lineList2.size()-1;  // strip end blank lines (for file 2)
   while (l2>=0 && ((String) lineList2.get(l2)).length()==0 ) 
   { lineList2.remove(l2--); }

   Date d = new Date();
   out.println(d+"\nComparing Files: "+file1+" "+file2+"\n");
   boolean correctFlag = true;

   if (l1 == l2) 
   {
       for (int i=0; i<=l1; i++)
       {
	   String s1 = lineList1.get(i).trim();
	   String s2 = lineList2.get(i).trim();
	  
           Integer d1, d2, d3;
	   try {
	     d1 = new Integer(s1);
	   }
	   catch (Exception e) { d1= new Integer(-1); } 
           try {
             StringTokenizer s2t = new StringTokenizer(s2);
	     d2 = new Integer(s2t.nextToken());
	     d3 = new Integer(s2t.nextToken());
	   }
	   catch (Exception e) { d2= new Integer(-2); d3= new Integer(-2); }
             teamValue = d1.intValue() ;
             targetValue = d2.intValue() ;
             judgeValue = d3.intValue() ;
    	     if ( Math.abs(judgeValue-targetValue) == Math.abs(teamValue-targetValue) ) { 
	        out.println("| "+s1);
	     }
	     else
	     { 
                correctFlag = false;
	        out.println("< "+s1); 
	        out.println("> "+s2); 
	     }
       }
   }
   else 
   { 
       correctFlag = false; 
       out.println("Number of output lines differ ("+
	       (l1+1)+" vs. "+(l2+1)+")!");
   }


   if (correctFlag) 
   { 
       if (argv.length==3) {
result.write("<result outcome = \"Yes\" security = \""+argv[2]+"\">");
result.write("\n</result>");
result.close();
       }
       out.println("____________");
       out.println("RIGHT ANSWER"); 
       out.close();
       System.exit(1); 
   }

   if (argv.length==3) {
result.write("<result outcome = \"No - Wrong Answer\" security = \""+argv[2]+"\">");
result.write("\n</result>");
result.close();
   }
   out.println("____________"); 
   out.println("WRONG ANSWER"); 
   out.close();

   } catch (Exception e) 
   { 
       System.err.println("Error writing result"+e); 
       System.exit(1); 
   }
 } // main

} // class vcheck
