import java.util.List;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.Arrays;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.lang.NumberFormatException;
import java.util.Collections;


public class FanMapMaker {

	private static class IntList extends ArrayList<Integer> {}
	
	private void printChargesArray(int[] distribution, int size) {
		int percent;
		System.out.println("temp2tcharge:");
		for(int i = 0; i <= size; i++) {
			//	retlw	.248	;31 PWM=97%
			percent = (100*i)/60;
			System.out.println("\tretlw\t\t." + distribution[percent] + "\t\t; " + i + " " + percent + "%");
		}
	}

	private void printChargesArrayStatAll(int[] distribution, int size) {
		int percent;
		System.out.println("temp2tcharge_tab:");
		for(int i = 0; i <= size; i++) {
			percent = (100*i)/size;
			System.out.println("\XORLW\t\t."+i+"\n"+
				"\tBTFSC\t\tSTATUS,Z\n"+
				"\tMOVLW\t\t."+distribution[percent]+"\t\t; [" + i + "] " + percent + "%\n"
			);
		}
		System.out.println("\tGOTO\t\ttemp2tcharge_tab_end");
	}

	private void printChargesArrayStatic(int[] distribution, int size) {
		int percent;
		String tmpRegName = "TMP0";
		String endLabel = "temp2tcharge_tab_end";
		System.out.println("temp2tcharge_tab:");
		
		System.out.println("\tMOVLW\t\t.255\n\tMOVF\t\t"+tmpRegName+",F\n\tBTFSS\t\tSTATUS,Z\n\tGOTO\t\tnext1\n\tGOTO\t\t"+endLabel);
		for(int i = 1; i <= size; i++) {
			//	retlw	.248	;31 PWM=97%
			percent = (100*i)/60;
			//System.out.println("\tretlw\t\t." + distribution[percent] + "\t\t; " + i + " " + percent + "%");
			System.out.println("next"+i+":\n\tMOVLW\t\t."+ distribution[percent]+"\t\t; [" + i + "] " + 
				percent + "%\n\tDECFSZ\t\t"+tmpRegName+",F\n\tBTFSS\t\tSTATUS,Z\n\tGOTO\t\t"+
				((i<size) ? "next"+(i+1) : endLabel) + "\n\tGOTO\t\t"+endLabel);
		}
	}
	
	public static void main(String[] a) {
		FanMapMaker fmp = new FanMapMaker();
		
		IntList percentToCharge[] = new IntList[101];
		int res[] = new int[101];
		for(int i = 0; i < percentToCharge.length; i++)
			percentToCharge[i] = new IntList();
		
		File decFile = new File("allDec.dat");
		File incFile = new File("allInc.dat");
		
		BufferedReader decReader = null;
		BufferedReader incReader = null;
		
		Integer tchrg;
		Integer fanPercx;
		Float sum;
		Float fanPerc;
		
		try {
			//decReader = new BufferedReader(new FileReader(decFile));
			//incReader = new BufferedReader(new FileReader(incFile));
			
			Scanner decScan = new Scanner(decFile);
			Scanner incScan = new Scanner(incFile);
			
			int cnt = 0;
			
			while (decScan.hasNext()) {
        		tchrg = decScan.nextInt();
        		fanPerc = 0.0f;
				for(int i = 0; i < 3; i++) {
					fanPerc += decScan.nextFloat();
	        	}
	        	fanPerc /= 3.0f;
	        	fanPercx = Math.round(fanPerc);
	        	percentToCharge[fanPercx].add(tchrg); //new Float(decScan.nextFloat())
	        	//System.out.println(tchrg + " " + fanPercx);
        	}

			while (incScan.hasNext()) {
        		tchrg = incScan.nextInt();
        		fanPerc = 0.0f;
				for(int i = 0; i < 3; i++) {
					fanPerc += incScan.nextFloat();
	        	}
	        	fanPerc /= 3.0f;
	        	fanPercx = Math.round(fanPerc);
	        	percentToCharge[fanPercx].add(tchrg); //new Float(decScan.nextFloat())
	        	//System.out.println(tchrg + " " + fanPercx);
        	}
        	
        	
        	
        	for (int i = 0; i < percentToCharge.length; i++) {
        		Collections.sort(percentToCharge[i]);
        		
        		//System.out.print("" + i + " " + "[");
        		for(Integer e: percentToCharge[i]) {
        			//System.out.print(e + " ");
        		}
        		//System.out.println("]");
        	}
        	
        	int last = 1;
        	for (int i = percentToCharge.length - 1; i >= 0; i--) {
        		if(percentToCharge[i].size() > 0) {
        			res[i] = percentToCharge[i].get(0);
        			last = res[i];
        		} else {
        			res[i] = last;
        		}
        		//System.out.println(i + " " + res[i]);
        	}
        	
        	fmp.printChargesArrayStatAll(res, 50);
        	
        	
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} finally {
			/*try {
				if (decReader != null) {
					//decReader.close();
				}
			} //catch (IOException e) {
			//}
			
			try {
				if (incReader != null) {
					//incReader.close();
				}
			} //catch (IOException e) {
			//}
			*/
		}
		
	}

}
