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

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Lance Chen
 */
public class CodeHelper {

    private static String FILE_ROOT = "D:/workspace/googlecodejam/meta";

    public static List<List<String>> loadInputsExcludes(String fileName) {
        List<List<String>> result = new ArrayList<List<String>>();
        List<String> lines = FileUtil.readLines(FILE_ROOT + fileName);
        int index = 0;
        for (String line : lines) {
            if (index == 0) {
                index++;
                continue;
            }
            if (index % 2 == 1) {
                index++;
                continue;
            }
            List<String> dataList = new ArrayList<String>();
            String[] dataArray = line.split("\\s+");
            for (String data : dataArray) {
                if (data.trim().length() > 0) {
                    dataList.add(data);
                }
            }
            result.add(dataList);
            index++;
        }
        return result;
    }

    public static List<List<String>> loadInputs(String fileName) {
        List<List<String>> result = new ArrayList<List<String>>();
        List<String> lines = FileUtil.readLines(FILE_ROOT + fileName);
        for (String line : lines) {
            List<String> dataList = new ArrayList<String>();
            String[] dataArray = line.split("\\s+");
            for (String data : dataArray) {
                if (data.trim().length() > 0) {
                    dataList.add(data);
                }
            }
            result.add(dataList);
        }
        return result;
    }

    public static List<String> loadInputLines(String fileName) {
        return FileUtil.readLines(FILE_ROOT + fileName);
    }

    public static void writeOutputs(String fileName, List<String> lines) {
        FileUtil.writeLines(FILE_ROOT + fileName, lines);
    }
}

package com.codejam.two12.GooglerDance;

import java.util.ArrayList;
import java.util.Arrays;

import com.jp.common.Puzzle;

public class GooglerDance implements Puzzle{

  private int numberOfGooglers;
  private int surprisingTriplets;
  private int parValue;
  private int[] totalPointArray;
  private int currentNumberOfSurprises;
  private ArrayList<Integer> ResultList;
  
  @Override
  public String[] solve(String[] dataSet) {
    // TODO Auto-generated method stub
    ResultList = new ArrayList<Integer>();
    processInput( dataSet);
    calculateResult();
    
    String result[] = new String[1];  
    result[0] = String.valueOf(ResultList.size());
    return result;
  }
  
  public void processInput(String[] dataSet){
    String data[] = dataSet[0].split(" ");
    numberOfGooglers = Integer.parseInt(data[0]);
    surprisingTriplets = Integer.parseInt(data[1]);
    parValue = Integer.parseInt(data[2]);
    
    totalPointArray = new int[numberOfGooglers];
    
    for(int i = 0; i < totalPointArray.length; i++){
      totalPointArray[i] = Integer.parseInt(data[ 3 + i]);
    }
    
    Arrays.sort(totalPointArray);
    
   }
  
  public void calculateResult(){
    
    for (int i = 0; i < totalPointArray.length; i++){
      int total = totalPointArray[i];
      int remainder = total / 3;
      int quotient = total % 3;
      
      int diff = parValue - remainder;
      
      // This cant be part of result. so skip and continue
      if(diff > 2 || (total == 0 && parValue != 0) ){
        continue;
      }
      else if(diff <= 0 ){
        ResultList.add(total);
        continue;
      }
      
      if(diff == 1 && quotient == 0 && currentNumberOfSurprises < surprisingTriplets){
        ++ currentNumberOfSurprises;
        ResultList.add(total);
      }
      else if(diff == 1 && quotient == 1 ){
        if(currentNumberOfSurprises < surprisingTriplets)
          ++ currentNumberOfSurprises;
        
        ResultList.add(total);
      }
      else if(diff == 1 && quotient == 2){
        ResultList.add(total);
      }
      else if(diff == 2 && quotient == 2 && currentNumberOfSurprises < surprisingTriplets){
        ++ currentNumberOfSurprises;
        ResultList.add(total);
      }
    }
    
    
  }
  
}
