Computer Programming Contest Preparation

ToolBox - Source for: 4/489/a.c



/home/toolbox/public_html/solutions/4/489/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: 2019-09-11
   18  * Purpose: fun
   19  * Problem: 489
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_LINE 2048
   27 #define MAX_WRONG 6
   28 #define notGuessed ((wordInt & guessInt) != wordInt)
   29 
   30 int roundNum;
   31 char buff[MAX_LINE];
   32 int wordInt;
   33 int guessInt;
   34 int lngth;
   35 int twos[32] =  /*        1           2           4           8 */
   36 {
   37     0x00000001, 0x00000002, 0x00000004, 0x00000008,
   38     /*        16          32          64         128 */
   39     0x00000010, 0x00000020, 0x00000040, 0x00000080,
   40     /*       256         512        1024        2048 */
   41     0x00000100, 0x00000200, 0x00000400, 0x00000800,
   42     /*      4096        8192       16384       32768 */
   43     0x00001000, 0x00002000, 0x00004000, 0x00008000,
   44     /*     65636      131072      262144      524288 */
   45     0x00010000, 0x00020000, 0x00040000, 0x00080000,
   46     /*   1048576     2097152     4194304     8388608 */
   47     0x00100000, 0x00200000, 0x00400000, 0x00800000,
   48     /*  16777216    33554432    67108864   134217728 */
   49     0x01000000, 0x02000000, 0x04000000, 0x08000000,
   50     /* 268435456   536870912  1073741824  2147483648 */
   51     0x10000000, 0x20000000, 0x40000000, 0x80000000
   52 };
   53 
   54 
   55 int getInput()
   56 {
   57     /* FUNCTION getInput */
   58     int dataReadFlag;
   59     int i;
   60 
   61     scanf(" %d ", &roundNum);
   62     dataReadFlag = (-1 != roundNum);
   63     if (dataReadFlag)
   64         {
   65             /* read in two lines */
   66             scanf(" %s ", buff);
   67             lngth = strlen(buff);
   68             wordInt = 0;
   69             for (i=0; lngth>i; i++)
   70                 {
   71                     /* process each letter */
   72                     wordInt = wordInt | twos[buff[i] - 'a'];
   73                 } /* process each letter */
   74             scanf(" %s ", buff);
   75         } /* read in two lines */
   76     return (dataReadFlag);
   77 } /* FUNCTION getInput */
   78 
   79 void process()
   80 {
   81     /* FUNCTION process */
   82     int i;
   83     int bit;
   84     int wrong = 0;
   85 
   86     printf("Round %d\n", roundNum);
   87 
   88     i = 0;
   89     lngth = strlen(buff);
   90     guessInt = 0;
   91     while ((notGuessed) && (MAX_WRONG >= wrong) && (lngth>i))
   92         {
   93             /* while */
   94             bit = twos[buff[i] - 'a'];
   95             if ((0 == (bit & guessInt)) && (0 == (bit & wordInt)))
   96                 {
   97                     /* new letter guessed  that is wrong */
   98                     wrong++;                   /* increment count of wrong letters */
   99                 } /* new letter guessed  that is wrong */
  100             /* wrong = wrong + ((0 == (bit & guessInt)) && (0 == (bit & wordInt))) ? 1 : 0; */
  101             guessInt = guessInt | bit; /* mark letter as guessed */
  102             i = i + 1;
  103         } /* while */
  104 
  105     if (notGuessed)
  106         {
  107             /* not guessed */
  108             if (MAX_WRONG < wrong)
  109                 {
  110                     printf("You lose.\n");
  111                 }
  112             else
  113                 {
  114                     printf("You chickened out.\n");
  115                 }
  116         } /* not guessed */
  117     else
  118         {
  119             /* guessed it right */
  120             printf("You win.\n");
  121         } /* guessed it right */
  122 } /* FUNCTION process */
  123 
  124 int main()
  125 {
  126     /* main */
  127     int moreToDo;
  128 
  129     moreToDo = getInput();
  130     while (moreToDo)
  131         {
  132             /* while */
  133             process();
  134             moreToDo = getInput();
  135         } /* while */
  136 
  137     return EXIT_SUCCESS;
  138 } /* main */
  139