/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package IO;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
    
/**
 * 
 * @author dannocz
 */
public class InputReader {
    
    public static Input readFile(String filename){
        try {
              
              /*  Sets up a file reader to read the file passed on the command
          77        line one character at a time */
              FileReader input = new FileReader(filename);
              
              /* Filter FileReader through a Buffered read to read a line at a
                 time */
              BufferedReader bufRead = new BufferedReader(input);
              
              String line;    // String that holds current file line
              int count = 0;  // Line number of count 
              ArrayList<String> cases= new ArrayList<String>();
              
              // Read first line
              line = bufRead.readLine();
              int noTestCases=Integer.parseInt(line);
              count++;
              
              // Read through file one line at time. Print line # and line
              while (count<=noTestCases){
                  //System.out.println("Reading. "+count+": "+line);
                  line = bufRead.readLine();
                  cases.add(line);
                  count++;
              }
              
              bufRead.close();
              
              return new Input(noTestCases,cases);
              
          }catch (ArrayIndexOutOfBoundsException e){
              /* If no file was passed on the command line, this expception is
              generated. A message indicating how to the class should be
              called is displayed */
              e.printStackTrace();        
  
          }catch (IOException e){
              // If another exception is generated, print a stack trace
              e.printStackTrace();
          }
        
        return null;
          
      }// end main
    }

package br.com.atama.google.jam.dancing;

public class TripletScore implements Comparable<TripletScore> {

	private final int threshold = Context.INSTANCE.getThreshold();
	private final int total;
	private final int mod;
	private final int gap;
	private int potential = 0;
	private int numGoodScore = 0;
	private boolean isSurprise = false;

	public TripletScore(int total) {
		this.total = total;
		final int div = total / 3;
		mod = total % 3;
		gap = threshold - div;

		calculateGoodScore();
	}

	private void calculateGoodScore() {
		if (total == 0) {
			numGoodScore = 0;
			isSurprise = false;
			
			if (gap <= 0)
				numGoodScore = 3;
			
			potential = Integer.MIN_VALUE;
		} else if (isSurprise) {
			numGoodScore = 0;
			if (mod == 0) {
				if (gap == 1) {
					numGoodScore = 1;
					potential = -1;
				} else if (gap == 0) {
					numGoodScore = 2;
					potential = 1;
				} else if (gap < 0) {
					numGoodScore = 3;
					potential = 0;
				}
			} else if (mod == 1) {
				if (gap == 1) {
					numGoodScore = 2;
					potential = -1;
				} else if (gap == 0) {
					numGoodScore = 2;
					potential = 1;
				} else if (gap < 0) {
					numGoodScore = 3;
					potential = 0;
				}
			} else if (mod == 2) {
				if (gap == 2) {
					numGoodScore = 1;
					potential = -1;
				} else if (gap == 1) {
					numGoodScore = 1;
					potential = 1;
				} else if (gap <= 0) {
					numGoodScore = 3;
				}
			} else {
				assert false;
			}
		} else {
			numGoodScore = 0;
			if (mod == 0) {
				if (gap == 1) {
					potential = 1;
				} else if (gap == 0) {
					numGoodScore = 3;
					potential = -1;
				} else if (gap < 0) {
					numGoodScore = 3;
				}
			} else if (mod == 1) {
				if (gap == 1) {
					numGoodScore = 1;
					potential = 1;
				} else if (gap == 0) {
					numGoodScore = 3;
					potential = -1;
				} else if (gap < 0) {
					numGoodScore = 3;
					potential = 0;
				}
			} else if (mod == 2) {
				if (gap == 2) {
					potential = 1;
				} else if (gap == 1) {
					numGoodScore = 2;
					potential = -1;
				} else if (gap <= 0) {
					numGoodScore = 3;
					potential = 0;
				}
			} else {
				assert false;
			}
		}
	}

	@Override
	public int compareTo(TripletScore o) {
		if (numGoodScore == 0) {
			if (o.numGoodScore == 0)
				return potential - o.potential;
			return 1;
		} else {
			if (o.numGoodScore == 0)
				return -1;
			return potential - o.potential;
		}
	}

	public boolean isSurprise() {
		return isSurprise;
	}

	public void setSurprise(boolean b) {
		isSurprise = b;
		calculateGoodScore();
	}

	public int getNumGoodScore() {
		return numGoodScore;
	}

	public boolean isZero() {
		return total == 0;
	}

}
