
/* 
(c) 1995 by Dan Goldwater.  12/16/95
computes word value for a word puzzle contest.

reads input from stdin.
*/

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>



/*** this defines the value of each letter ***/

// puzzle 1: "video game" from Pandemonium, Inc.
// enum {NUL = 0, f,u,a,i,o, e,y,x,d,g, s,m,b,l,r, v,k,z,t,c, n,p,j,h,w, q};

// puzzle 2: "media play" from Pandemonium, Inc.
// enum {NUL = 0, x,p,u,y,l, a,o,i,e,m, v,b,j,k,q, t,r,z,h,c, d,s,n,w,f, g};

// puzzle 3: "computer" from Pandemonium, Inc.
enum {NUL = 0, v,g,k,a,u, y,e,i,b,o, x,f,d,n,q, m,r,c,w,z, t,s,l,p,h, j};



// lookup array for letter values
int letter_value[26] = {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z};


int main(int argc, char *argv[])
{
	int i,j;
	char str1[40];			// input word before converting to lowercase
	char *word1;			// the input word
	int word_length;
	FILE *word_grid_file;		// the input data file
	int value_grid[10][6];
	int row_value[6];
	int row_total;
	int force_div;
	int force_mult;
	int power_col;
	float grand_total;

	/* open input file in read-only mode */
	if((word_grid_file = fopen(argv[1], "r")) == NULL)
	{
		printf("you must specify a data file\n");
		return 1;
	}

	/* read data grid from file into internal storage */
		/* while(!feof(word_grid_file)) */

	row_total = 0;
	for(i=0; i<6; i++)
	{
		fscanf(word_grid_file, "%s", str1);
		word1 = _strlwr(_strdup(str1));  // convert to lower case
		//		word_length = strlen(word1);

		row_value[i] = 0;
		for(j=0; j<10; j++)
		{
			value_grid[j][i]=letter_value[word1[j] - 97];
			row_value[i] += value_grid[j][i];
		}
		row_total += row_value[i];
	}
	
	force_div = row_value[0];

	force_mult = 0;
	power_col = 0;
	for(i=0; i<6; i++)
	{
		force_mult += value_grid[0][i];
		power_col += value_grid[2][i];
	}

	grand_total = (((float)(row_total + power_col) * force_mult) / force_div);

	/* print output */
	printf("sum of rows: %u + %u + %u + %u + %u + %u = %u = row total\n", row_value[0], row_value[1], row_value[2], row_value[3], row_value[4], row_value[5], row_total);
	printf("(((%u + %u) * %u) / %u) = %6.3f = grand total\n", row_total, power_col, force_mult, force_div, grand_total);

	fclose(word_grid_file);
	return 0;
}
