Computer Programming Contest Preparation

ToolBox - Source for: 3/340/a.c



/home/toolbox/public_html/solutions/3/340/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: 2017-01-22
   18  * Purpose: fun
   19  * Problem: 340 Master-Mind Hints
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_SIZE 1001
   27 
   28 int sz;
   29 int save[MAX_SIZE];
   30 int secret[MAX_SIZE];
   31 int guess[MAX_SIZE];
   32 
   33 void init()
   34 {
   35     /* FUNCTION init */
   36 } /* FUNCTION init */
   37 
   38 void dump()
   39 {
   40     /* FUNCTION dump */
   41     int i;
   42 
   43     printf("secret:");
   44     for (i=0; i<sz; i++)
   45         {
   46             printf(" %d", secret[i]);
   47         }
   48     printf("\n");
   49     printf(" guess:");
   50     for (i=0; i<sz; i++)
   51         {
   52             printf(" %d", guess[i]);
   53         }
   54     printf("\n");
   55 } /* FUNCTION dump */
   56 
   57 int getInput()
   58 {
   59     /* FUNCTION getInput */
   60     int dataReadFlag;
   61 
   62     scanf(" %d ", &sz);
   63     dataReadFlag = (0 != sz);
   64     return (dataReadFlag);
   65 } /* FUNCTION getInput */
   66 
   67 int getGuess()
   68 {
   69     /* FUNCTION getGuess */
   70     int dataReadFlag;
   71     int i;
   72 
   73     for (i=0; i<sz; i++)
   74         {
   75             /* read each guess */
   76             scanf(" %d ", &guess[i]);
   77         } /* read each guess */
   78 
   79     dataReadFlag = (0 != guess[0]);
   80     return (dataReadFlag);
   81 } /* FUNCTION getGuess */
   82 
   83 int checkStrong()
   84 {
   85     /* FUNCTION checkStrong */
   86     int i;
   87     int tot = 0;
   88 
   89     for (i=0; i<sz; i++)
   90         {
   91             /* for */
   92             if (secret[i] == guess[i])
   93                 {
   94                     /* match found */
   95                     tot++;
   96                     secret[i] = 0;
   97                     guess[i] = 0;
   98                 } /* match found */
   99         } /* for */
  100     return tot;
  101 } /* FUNCTION checkStrong */
  102 
  103 int checkWeak()
  104 {
  105     /* FUNCTION checkWeak */
  106     int i;
  107     int k;
  108     int tot = 0;
  109 
  110     for (i=0; i<sz; i++)
  111         {
  112             /* outer loop */
  113             for (k=0; k<sz; k++)
  114                 {
  115                     /* inner loop */
  116                     if ((0 != secret[i]) && (secret[i] == guess[k]))
  117                         {
  118                             /* weak match found */
  119                             tot++;
  120                             secret[i] = 0;
  121                             guess[k] = 0;
  122                         } /* weak match found */
  123                 } /* inner loop */
  124         } /* outer loop */
  125     return tot;
  126 } /* FUNCTION checkWeak */
  127 
  128 void getSecret()
  129 {
  130     /* FUNCTION getSecret */
  131     int i;
  132 
  133     for (i=0; i<sz; i++)
  134         {
  135             /* for */
  136             scanf(" %d ", &save[i]);
  137         } /* for */
  138 } /* FUNCTION getSecret */
  139 
  140 void setSecret()
  141 {
  142     /* FUNCTION setSecret */
  143     int i;
  144 
  145     for (i=0; i<sz; i++)
  146         {
  147             /* for */
  148             secret[i] = save[i];
  149         } /* for */
  150 } /* FUNCTION setSecret */
  151 
  152 void process()
  153 {
  154     /* FUNCTION process */
  155     int strong;
  156     int weak;
  157     int anotherGuess;
  158 
  159     getSecret();
  160     anotherGuess = getGuess();
  161     while (anotherGuess)
  162         {
  163             /* anotherGuess */
  164             setSecret();
  165             DEBUG dump();
  166             strong = checkStrong();
  167             weak = checkWeak();
  168             DEBUG dump();
  169             printf("    (%d,%d)\n", strong, weak);
  170             anotherGuess = getGuess();
  171         } /* anotherGuess */
  172 } /* FUNCTION process */
  173 
  174 int main()
  175 {
  176     /* main */
  177     int moreToDo;
  178     int cnt = 1;
  179 
  180     moreToDo = getInput();
  181     while (moreToDo)
  182         {
  183             /* while */
  184             printf("Game %d:\n", cnt);
  185             cnt++;
  186             process();
  187             moreToDo = getInput();
  188         } /* while */
  189 
  190     return EXIT_SUCCESS;
  191 } /* main */
  192