Computer Programming Contest Preparation

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



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