#include "_.h"

int get_number_input_to_wint(int A[], int B[], int prize[]) {
    int i = 0, j = 0; 
    int min = 1000;
    for (; i < 1000; i++) {
        j = 0;
        for (; j < 1000; j++) {
            if ((A[0] * i) + (B[0] * j) == prize[0]
                && (A[1] * i) + (B[1] * j) == prize[1])
                min = MIN(i * 3 + j, min);
        }
    }
    //printf("tokens: %d\n\n", min);
    return min;
}

int main(int argc, char **argv) {
	
	if (argc != 2)
		return 1;

	char *input = argv[1];
	char **inputs = str_split(strdup(input), '\n');

    int token_spent = 0;
	for (int i = 0; inputs[i] != NULL; i+=3) {
	    char **button_A = str_split(strdup(inputs[i]), ' ');
        int A[2] = {atoi(button_A[0]), atoi(button_A[1])};  

	    char **button_B = str_split(strdup(inputs[i + 1]), ' ');
        int B[2] = {atoi(button_B[0]), atoi(button_B[1])};  

	    char **prizes = str_split(strdup(inputs[i + 2]), ' ');
        int prize[2] = {atoi(prizes[0]), atoi(prizes[1])};  

        int number = get_number_input_to_wint(A, B, prize);
        token_spent += number == 1000 ? 0 : number;
	}

    printf("token_spent: %d\n", token_spent);

	return 0;
}
