Computer Programming Contest Preparation

ToolBox - Source for: 101/10149/a.c



/home/toolbox/public_html/solutions/101/10149/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 #define NUMROLLS 13
   16 
   17 int ary[15];
   18 int buff[5];
   19 int dice[7];
   20 int moreToDo;
   21 
   22 
   23 /*
   24  *  Author: Isaac Traxler, Josh Abadie
   25  *    Date: 20080327
   26  * Purpose:
   27  * Problem: 10149
   28  */
   29 
   30 void init()
   31 {
   32     /* FUNCTION init */
   33     int i;
   34 
   35     for (i=0; 15 > i; i++)
   36         {
   37             /* for */
   38             ary[i] = 0;
   39         } /* for */
   40 } /* FUNCTION init */
   41 
   42 int getInput()
   43 {
   44     /* FUNCTION getInput */
   45     int dataReadFlag;
   46 
   47     dataReadFlag = (EOF != scanf(" %d %d %d %d %d ", &buff[0], &buff[1], &buff[2], &buff[3], &buff[4]));
   48     return (dataReadFlag);
   49 } /* FUNCTION getInput */
   50 
   51 int sum(int * roll)
   52 {
   53     /* FUNCTION sum */
   54     int i;
   55     int toReturn = 0;
   56 
   57     for (i=0; 5>i; i++)
   58         {
   59             /* for */
   60             toReturn = toReturn + roll[i];
   61         } /* for */
   62 
   63     return (toReturn);
   64 } /* FUNCTION sum */
   65 
   66 int exactTest(int * roll, int cnt)
   67 {
   68     /* FUNCTION exactTest */
   69     int i;
   70     int toReturn = 0;
   71 
   72     for (i=1; 7>i; i++)
   73         {
   74             /* for */
   75             if (dice[i] == cnt)
   76                 {
   77                     toReturn = i;
   78                 }
   79         } /* for */
   80 
   81     return (toReturn);
   82 } /* FUNCTION exactTest */
   83 
   84 int anyTest(int * roll, int cnt)
   85 {
   86     /* FUNCTION anyTest */
   87     int i;
   88     int toReturn = 0;
   89 
   90     for (i=1; 7>i; i++)
   91         {
   92             /* for */
   93             if (dice[i] >= cnt)
   94                 {
   95                     toReturn = i;
   96                 }
   97         } /* for */
   98 
   99     return (toReturn);
  100 } /* FUNCTION anyTest */
  101 
  102 int doFirstSix(int * roll, int n)
  103 {
  104     /* FUNCTION doFirstSix */
  105     int toReturn = 0;
  106     int i;
  107 
  108     for (i=0; 5>i; i++)
  109         {
  110             /* for */
  111             if (n == roll[i])
  112                 {
  113                     toReturn = toReturn + n;
  114                 }
  115         } /* for */
  116     return (toReturn);
  117 } /* FUNCTION doFirstSix */
  118 
  119 int smallStraight()
  120 {
  121     /* FUNCTION smallStraight */
  122     /* possible small straights:
  123        1234
  124         2345
  125          3456
  126        So 3 and 4 must always be present and 2 others
  127     */
  128     return ( ((0<dice[3]) && (0<dice[4])) && ( ((0<dice[1]) && (0<dice[2])) || ((0<dice[2]) && (0<dice[5])) || ((0<dice[5]) && (0<dice[6])) ));
  129 } /* FUNCTION smallStraight */
  130 
  131 int largeStraight()
  132 {
  133     /* FUNCTION largeStraight */
  134     /* possible large straights:
  135        12345
  136         23456
  137        So 3, 4 and 5 must always be present and 2 others
  138     */
  139     return (((0<dice[3]) && (0<dice[4]) && (0<dice[5]))  &&  ( (0<dice[1]) || (0<dice[6]) ));
  140 } /* FUNCTION largeStraight */
  141 
  142 void doRoll(int * roll, int r)
  143 {
  144     /* FUNCTION doRoll */
  145     int i;
  146 
  147     /* zero out counts for each possible face */
  148     for (i=1; 7>i; i++)
  149         {
  150             dice[i] = 0;
  151         }
  152     /* process each die and see what value it has */
  153     for (i=0; 5>i; i++)
  154         {
  155             dice[roll[i]] = dice[roll[i]] + 1;
  156         }
  157 
  158     DEBUG { printf("roll:"); for (i=0; 5>i; i++)
  159     {
  160         printf(" %d", roll[i]);
  161         } printf("\n"); }
  162     DEBUG { printf("dice:"); for (i=1; 7>i; i++)
  163     {
  164         printf(" %d", dice[i]);
  165         } printf("\n"); }
  166 
  167     switch (r)
  168         {
  169         /* switch */
  170         case 0:
  171         case 1:
  172         case 2:
  173         case 3:
  174         case 4:
  175         case 5:
  176             /* cases 1-6 */
  177             ary[r] = doFirstSix(roll, (r+1));
  178             /* cases 1-6 */
  179             break;
  180         case 6: /* chance */
  181             ary[r] = sum(roll);
  182             break;
  183         case 7: /* 3 of a kind */
  184             if (0 != anyTest(roll, 3))
  185                 {
  186                     /* 3 of a kind */
  187                     ary[r] = sum(roll);
  188                 } /* 3 of a kind */
  189             break;
  190         case 8: /* four of a kind */
  191             if (0 != anyTest(roll, 4))
  192                 {
  193                     /* 4 of a kind */
  194                     ary[r] = sum(roll);
  195                 } /* 4 of a kind */
  196             break;
  197         case 9: /* 5 of a kind */
  198             if (0 != exactTest(roll, 5))
  199                 {
  200                     ary[r] = 50;    /* 5 of a kind */
  201                 }
  202             break;
  203         case 10:
  204             if (smallStraight())
  205                 {
  206                     ary[r] = 25;
  207                 }
  208             break;
  209         case 11:
  210             if (largeStraight())
  211                 {
  212                     ary[r] = 35;
  213                 }
  214             break;
  215         case 12:
  216             if ((0 != exactTest(roll, 2)) && (0 != exactTest(roll, 3)))
  217                 {
  218                     /* found a full hous */
  219                     ary[r] = 40;
  220                 } /* found a full hous */
  221             break;
  222         } /* switch */
  223 } /* FUNCTION doRoll */
  224 
  225 void process()
  226 {
  227     /* FUNCTION process */
  228     int i;
  229 
  230     for (i=0; NUMROLLS > i; i++)
  231         {
  232             /* for each roll */
  233             doRoll(buff, i);
  234             moreToDo = getInput();
  235         } /* for each roll */
  236 
  237     /* check for bonus */
  238     if (62 < (ary[0] + ary[1] + ary[2] + ary[3] + ary[4] + ary[5]))
  239         {
  240             ary[13] = 35;
  241         }
  242     /* sum up final score and output */
  243     for (i=0; 14>i; i++)
  244         {
  245             /* for */
  246             printf("%d ", ary[i]);
  247             ary[14] = ary[14] + ary[i];
  248         } /* for */
  249     printf("%d\n", ary[14]);
  250 } /* FUNCTION process */
  251 
  252 int main ()
  253 {
  254     /* main */
  255     moreToDo = getInput();
  256     while (moreToDo)
  257         {
  258             /* while */
  259             init();
  260             process();
  261         } /* while */
  262 
  263     return EXIT_SUCCESS;
  264 } /* main */
  265