Computer Programming Contest Preparation

ToolBox - Source for: 1/102/a.c



/home/toolbox/public_html/solutions/1/102/a.c
    1 #include <stdio.h>
    2 #include <string.h>
    3 #include <sys/types.h>
    4 #include <sys/stat.h>
    5 #include <fcntl.h>
    6 #include <stdint.h>
    7 #include <math.h>
    8 #include <stdlib.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 /*
   16  *  Author: Isaac Traxler
   17  *    Date: 2014-10-27
   18  * Purpose: fun
   19  * Problem: 102 - Ecological Bin Packing
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 int bin[3][3];
   27 char colors[3] = { 'B', 'G', 'C' };
   28 int sums[3];
   29 int combos[6][3] = { { 0, 2, 1 },    /* B C G */
   30     { 0, 1, 2 },    /* B G C */
   31     { 2, 0, 1 },    /* C B G */
   32     { 2, 1, 0 },    /* C G B */
   33     { 1, 0, 2 },    /* G B C */
   34     { 1, 2, 0 }
   35 };  /* G C B */
   36 
   37 void init()
   38 {
   39     /* FUNCTION init */
   40 } /* FUNCTION init */
   41 
   42 void dump()
   43 {
   44     /* FUNCTION dump */
   45 } /* FUNCTION dump */
   46 
   47 int getInput()
   48 {
   49     /* FUNCTION getInput */
   50     int dataReadFlag;
   51 
   52     if (feof(stdin))
   53         dataReadFlag = FALSE;
   54     else
   55         {
   56             /* something to read */
   57             dataReadFlag = TRUE;
   58             scanf(" %d %d %d %d %d %d %d %d %d ", &bin[0][0], &bin[0][1], &bin[0][2], &bin[1][0], &bin[1][1], &bin[1][2], &bin[2][0], &bin[2][1], &bin[2][2]);
   59         } /* something to read */
   60     return (dataReadFlag);
   61 } /* FUNCTION getInput */
   62 
   63 void doSums()
   64 {
   65     /* FUNCTION doSums */
   66     int i;
   67 
   68     for (i=0; i<3; i++)
   69         {
   70             /* do each sum */
   71             sums[i] = bin[0][i] + bin[1][i] + bin[2][i];
   72         } /* do each sum */
   73 } /* FUNCTION doSums */
   74 
   75 int score(int idx)
   76 {
   77     /* FUNCTION score */
   78     int tot = 0;
   79     int i;
   80 
   81     for (i=0; i<3; i++)
   82         {
   83             /* for each bin */
   84             tot = tot + sums[i] - bin[i][combos[idx][i]];
   85         } /* for each bin */
   86     return tot;
   87 } /* FUNCTION score */
   88 
   89 void process()
   90 {
   91     /* FUNCTION process */
   92     int best;
   93     int choice = 0;
   94     int tmp;
   95     int i;
   96 
   97     doSums();
   98     best = score(0);
   99     for (i=1; i<6; i++)
  100         {
  101             /* try each possible combo */
  102             tmp = score(i);
  103             DEBUG printf("i=%d score=%d sums(%d, %d, %d) best=%d choice=%d\n", i, tmp, sums[0], sums[1], sums[2], best, choice);
  104             if (tmp < best)
  105                 {
  106                     /* found a better choice */
  107                     best = tmp;
  108                     choice = i;
  109                 } /* found a better choice */
  110         } /* try each possible combo */
  111     printf("%c%c%c %d\n", colors[combos[choice][0]], colors[combos[choice][1]], colors[combos[choice][2]], best);
  112 } /* FUNCTION process */
  113 
  114 int main ()
  115 {
  116     /* main */
  117     int moreToDo;
  118 
  119     init();
  120     moreToDo = getInput();
  121     while (moreToDo)
  122         {
  123             /* while */
  124             process();
  125             moreToDo = getInput();
  126         } /* while */
  127 
  128     return EXIT_SUCCESS;
  129 } /* main */
  130