/*
 * THIS CODE IS RELEASED UNDER THE GNU GPL LICENSE AND COMES WITH NO WARRANTY
 * (C) 2004 Achille Astolfi
 */
import java.io.*;
import java.util.StringTokenizer;
/**
 * Redcoders Frenzy Tournament, Round #17 - Scheduler for the Big Brother Round.
 * Feel free to change this code as it suits you.
 * <p>STEPS:
 * <ol>
 * <li> Verify that "bully.red" and "little.red" reside in the same directory as the
 * scheduler, and that pmars.exe is in the path or in the same directory
 * <li> under Windows: open a DOS command, and run pmars once before launching the scheduler
 * (you simply need to type pmars and hit enter; this will display the usage lines, but
 * more important this configures the DOS command window so it can launch pmars under a
 * Java Process)
 * <li> launch the scheduler: if your warrior is big.red, the command is:
 * java -classpath . BigBrother big.red
 * <li> The output is: the first three lines are the KOTH format of the fight,
 * the fourth line is score little+big little all little+bully big bully+big bully.
 * The scheduler will be run 4 times for each warrior, and the total score for the
 * round will be the sum of all 4 scores.
 * </ol>
 * The score is 1 point for little+bully 2 points for all, 3 points
 * for little, 6 points for little+big; 0 points for all others.
 * @author Achille Astolfi
 * @version 1.0.0.3
 */
public class BigBrother {
	/**
	 * The main program.
	 */
	public static void main(String[] arg) throws Exception {
		final int BIG = 0x1;
		final int LITTLE = 0x2;
		final int BULLY = 0x4;
		final int BIG_LITTLE = BIG + LITTLE;
		final int BIG_BULLY = BIG + BULLY;
		final int LITTLE_BULLY = LITTLE + BULLY;
		final int ALL = BIG + LITTLE + BULLY;

		//usage
		if (arg.length != 1) {
			System.out.println("Usage: java -classpath . BigBrother file");
			return;
		}

		//the score and the counts in the order they will display
		int[] scores = new int[8];

		try {
			//run pmars and wait for end
			Process pr = Runtime.getRuntime().exec("pmars -bk -r 250 bully.red little.red " + arg[0]);
			pr.waitFor();

			//get ready to read the results
			InputStream is = pr.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));

			//see the results of the fight:
			//method parseLine returns the counts of only-one one-of-2 and all
			int[] bully = parseLine(br.readLine());
			int[] little = parseLine(br.readLine());
			int[] big = parseLine(br.readLine());

			//place 1 (big) 2 (little) 4 (bully) and 7 (all) are easy to compute
			scores[BIG] = big[0];
			scores[LITTLE] = little[0];
			scores[BULLY] = bully[0];
			scores[ALL] = bully[2]; //assuming this is the same value of little[2] and big[2]

			//now we must compute x = big + little (place 3) y = big + bully (place 5)
			//and z = little + bully (place 6)
			//we now that x + y = big[1] x + z = little[1] y + z = bully[1]
			//this system is always resolvable, and this is the result:
			//sum = x + y + z and is easily computed this way:
			int sum = (big[1] + little[1] + bully[1]) / 2; //the value in parenthesys is always even!

			//x = (x + y + z) - (y + z)
			scores[BIG_LITTLE] = sum - bully[1];
			//y = (x + y + z) - (x + z)
			scores[BIG_BULLY] = sum - little[1];
			//z = (x + y + z) - (x + y)
			scores[LITTLE_BULLY] = sum - big[1];

		} catch (Exception ioe) {
			//if any exception occurs, set the score to -1
			ioe.printStackTrace();
			scores[0] = -1;
		}

		//if the score is 0 i.e. no errors occurred calculate the score
		//1.0.0.2 changed the score
		if (scores[0] == 0) {
			//1 point for bully+little (place 6)
			//2 points for all (place 7)
			//3 points for little (place 2)
			//6 points for big + little (place 3)
			scores[0] = scores[LITTLE_BULLY] + scores[ALL] * 2 + scores[LITTLE] * 3 + scores[BIG_LITTLE] * 6;
		}

		//print out the score and the counts
		//1.0.0.3 changed the output line
		System.out.println(	scores[0] + " " + scores[BIG_LITTLE] + " " + scores[LITTLE] + " " +
							scores[ALL] + " " + scores[LITTLE_BULLY] + " " + scores[BIG] + " " +
							scores[BIG_BULLY] + " " + scores[BULLY]);
	}

	/**
	 * Parses a line and returns the counts of only-one, one-of-2, and one-of-3.
	 * The output of pmars is in the KOTH format: score only-one one-of-2 one-of-3 loses
	 * This method discards the first number, and builds an array
	 * out of the next 3 numbers.
	 */
	private static int[] parseLine(String line) {
		System.err.println(line);
		//tokenize the line
		StringTokenizer st = new StringTokenizer(line);

		//discard first token
		st.nextToken();

		//build the array
		int[] result = {
			//the second number
			Integer.parseInt(st.nextToken()),
			//the third number
			Integer.parseInt(st.nextToken()),
			//the fourth number
			Integer.parseInt(st.nextToken())
		};

		//return the result
		return result;
	}
}
