#include <stdlib.h>
#include <stdio.h>


//1.5*15 + 2*120 + 22.5 * 91 / 26 = 88.846


int main(int argc, char **argv){
	//double octanes[] = {};
	//double gallons[] = {};

	double octane_kerosene = 88.5;//15;
	double octane_naphtha = 55;
	double octane_petrol = 91;//86.011;
	double octane_xylene = 115;
	double octane_acetone = 120;
	double octane_denatured = 109;
	double octane_methanol = 113;
	double octane_diesel = 85.32;//14;

	double gallons_kerosene = 10.75;
	double gallons_naphtha = 0;//0.859375;
	double gallons_petrol = 0.0;
	double gallons_xylene = 1.0;
	double gallons_acetone = 0.0;
	double gallons_denatured = 0.25;
	double gallons_methanol = 0.25; //0.21875;
	double gallons_diesel = 6.808;

	double octane_mix = 0.0;
	double gallons_mix = 0.0;
	double octane_target = 87;
//	double gallons_target = 0.0;

	double gallons_capacity = 26;
	double octane_out = 0;
	double tmp1 = 0;

	gallons_mix = gallons_diesel+gallons_kerosene+gallons_naphtha+gallons_xylene+gallons_acetone+gallons_denatured+gallons_methanol;
	octane_mix = gallons_diesel*octane_diesel + gallons_kerosene*octane_kerosene + gallons_naphtha*octane_naphtha + gallons_xylene*octane_xylene + gallons_acetone*octane_acetone + gallons_denatured*octane_denatured + gallons_methanol*octane_methanol;
	octane_mix /= gallons_mix;

	printf("mix gallons: %0.3lf mix octane: %0.3lf\n", gallons_mix, octane_mix);
	//gallons_target*octane_mix + (gallons_capacity-gallons_target)*octane_petrol / gallons_capacity = octane_target

//T*M + (C-T)*P / C = O
//t=co−cpm−p


//octane_petrol = 100;
//octane_target = 75;
//octane_mix = 50;

	//gallons_target = gallons_capacity*octane_target - gallons_capacity*octane_petrol*octane_mix - octane_petrol;
	//gallons_target = gallons_capacity*octane_target - octane_petrol*gallons_petrol;
	//gallons_target /= gallons_mix;
	//gallons_target = gallons_capacity*octane_petrol - gallons_capacity*octane_target;
	//gallons_target /= octane_target - octane_mix;

//gallons_capacity*octane_target = gallons_mix*octane_mix + (gallons_capacity-gallons_mix)*octane_petrol;
//A*B = C*D + (A-C)*E;

//c=ab−af / d−f

	gallons_mix = gallons_capacity*octane_target - gallons_capacity*octane_petrol;
	gallons_mix /= octane_mix-octane_petrol;

	printf("mix gallons needed to produce %0.3lfg of octane %0.3lf : %0.3lfg\n", gallons_capacity, octane_target, gallons_mix);


	gallons_petrol = gallons_capacity-gallons_diesel-gallons_kerosene-gallons_naphtha-gallons_xylene-gallons_acetone-gallons_denatured-gallons_methanol;
	tmp1 = gallons_diesel*octane_diesel + gallons_kerosene*octane_kerosene + gallons_naphtha*octane_naphtha + gallons_petrol*octane_petrol + gallons_xylene*octane_xylene + gallons_acetone*octane_acetone + gallons_denatured*octane_denatured + gallons_methanol*octane_methanol;
	octane_out = tmp1/gallons_capacity;

	printf("(gallons) kerosene: %0.3lf naphtha: %0.3lf xylene: %0.3lf acetone: %0.3lf denatured: %0.3lf methanol: %0.3lf diesel: %0.3lf petrol: %0.3lf\n", 
		gallons_kerosene, gallons_naphtha, gallons_xylene, gallons_acetone, gallons_denatured, gallons_methanol, gallons_diesel, gallons_petrol);
	printf("total gallons: %0.3lf\n", gallons_capacity);
	printf("mix octane: %0.3lf\n", octane_out);


	return 0;
}
