Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/1/170/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 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 /*
   17  *  Author: Isaac Traxler
   18  *    Date: 2021-11-06
   19  * Purpose: fun
   20  * Problem: 1700 - Clock Patience
   21  */
   22 
   23 /*
   24  * This template reads data until a terminating value is reached.
   25  */
   26 
   27 /* encoding method:
   28  * '2'..'9' are 50..57 maps to 2..9
   29  *  'A' (ace) is 65 maps to 1
   30  *  'T' (ten) is 84 maps to 10
   31  *  'J' (jack) is 74 maps to 11
   32  *  'Q' (queen) is 81 maps to 12
   33  *  'K' (king) is 75 maps to 13
   34  *  'C' (club) is 67 maps to 16    0001 0000
   35  *  'D' (diamond) is 68 maps to 32 0010 0000
   36  *  'H' (heart) is 72 maps to 48   0011 0000
   37  *  'S' (spade) is 83 maps to 64   0100 0000
   38  *  A card will be hashed to a byte:
   39  *  bottom nibble will have vard value after mapping
   40  *  top nobble will have suit after mapping
   41  *
   42  *  AC - 0000 0001   9C - 0000 1001  KC - 0000 1101
   43  *  AD - 0001 0001   9D - 0001 1001  KD - 0001 1101
   44  *  AH - 0010 0001   9H - 0010 1001  KH - 0010 1101
   45  *  AS - 0011 0001   9S - 0011 1001  KS - 0011 1101
   46  *
   47  *  Since card will hold rank in first cahr and suit in second char
   48  *  map[char[0]] is the rank of the card and map[char[1]] is the suit of the card
   49  *  adding them togehter creates the hah value for the card
   50  */
   51 
   52 #define SUIT 0b01110000  /* mask to get upper nibble of byte */
   53 #define RANK 0b00001111  /* rank mask to get lower nibble of byte */
   54 
   55 char map[128];
   56 char prnt[66];
   57 char deck[14][5]; /* deck read in */
   58 char depth[14];   /* card to be turned next */
   59 char card[3];
   60 int cardsPlayed;
   61 
   62 void init()
   63 {
   64     /* FUNCTION init */
   65     map['A'] = 1;
   66     prnt[1] = 'A';
   67     map['2'] = 2;
   68     prnt[2] = '2';
   69     map['3'] = 3;
   70     prnt[3] = '3';
   71     map['4'] = 4;
   72     prnt[4] = '4';
   73     map['5'] = 5;
   74     prnt[5] = '5';
   75     map['6'] = 6;
   76     prnt[6] = '6';
   77     map['7'] = 7;
   78     prnt[7] = '7';
   79     map['8'] = 8;
   80     prnt[8] = '8';
   81     map['9'] = 9;
   82     prnt[9] = '9';
   83     map['T'] = 10;
   84     prnt[10] = 'T';
   85     map['J'] = 11;
   86     prnt[11] = 'J';
   87     map['Q'] = 12;
   88     prnt[12] = 'Q';
   89     map['K'] = 13;
   90     prnt[13] = 'K';
   91 
   92     map['C'] = 16;
   93     prnt[16] = 'C';
   94     map['D'] = 32;
   95     prnt[32] = 'D';
   96     map['H'] = 48;
   97     prnt[48] = 'H';
   98     map['S'] = 64;
   99     prnt[64] = 'S';
  100 
  101     map[' '] = 0;
  102     prnt[0] = ' ';
  103 
  104 } /* FUNCTION init */
  105 
  106 void reset()
  107 {
  108     /* FUNCTION reset */
  109     int i;
  110     int j;
  111 
  112     for (i=0; 14>i; i++)
  113         {
  114             /* for each pile */
  115             depth[i] = 4;
  116             for (j=0; 5>j; j++)
  117                 {
  118                     /* for each spot */
  119                     deck[i][j] = 0;
  120                 } /* for each spot */
  121         } /* for each pile */
  122     cardsPlayed = 0;
  123 } /* FUNCTION reset */
  124 
  125 int getInput()
  126 {
  127     /* FUNCTION getInput */
  128     int dataReadFlag;
  129     int i;
  130     int d;
  131     int k;
  132 
  133     scanf(" %s ", card);
  134     dataReadFlag = ('#' != card[0]);
  135     if (dataReadFlag)
  136         {
  137             /* read deck */
  138             i = 13;
  139             d = 4;
  140             deck[i][d] = map[card[0]] + map[card[1]];
  141             for (k=1; 52>k; k++)
  142                 {
  143                     /* get next 51 cards */
  144                     scanf(" %s ", card);
  145                     i--;
  146                     if (0 == i)
  147                         {
  148                             d--;
  149                             i=13;
  150                         }
  151                     deck[i][d] = map[card[0]] + map[card[1]];
  152                 } /* get next 51 cards */
  153         } /* read deck */
  154     return (dataReadFlag);
  155 } /* FUNCTION getInput */
  156 
  157 void dump()
  158 {
  159     /* FUNCTION dump */
  160     int i;
  161     int j;
  162 
  163     card[2] = 0;
  164     printf(" A  2  3  4  5  6  7  8  9  T  J  Q  K\n");
  165     for (j=4; 0<j; j--)
  166         {
  167             /* for each depth */
  168             for (i=1; 14>i; i++)
  169                 {
  170                     /* for each position */
  171                     card[0] = prnt[(RANK & deck[i][j])];
  172                     card[1] = prnt[(SUIT & deck[i][j])];
  173                     printf("%s ", card);
  174                 } /* for each position */
  175             printf("\n");
  176         } /* for each depth */
  177     for (i=1; 14>i; i++)
  178         {
  179             /* for each position */
  180             printf("  %d", depth[i]);
  181         } /* for each position */
  182     printf("\n");
  183 } /* FUNCTION dump */
  184 
  185 void process()
  186 {
  187     /* FUNCTION process */
  188     int currentRank;
  189     int currentCard;
  190     int previousCard;
  191 
  192     DEBUG dump();
  193     currentRank = 13;
  194     while (0 != depth[currentRank])
  195         {
  196             /* keep playing cards */
  197             cardsPlayed++;
  198             previousCard = currentCard;
  199             currentCard = deck[currentRank][depth[currentRank]];
  200             depth[currentRank] = depth[currentRank] - 1;
  201             currentRank = RANK & currentCard;
  202             DEBUG dump();
  203         } /* keep playing cards */
  204     DEBUG dump();
  205     card[0] = prnt[(RANK & currentCard)];
  206     card[1] = prnt[(SUIT & currentCard)];
  207     card[2] = 0;
  208     printf("%02d,%s\n", cardsPlayed, card);
  209 } /* FUNCTION process */
  210 
  211 int main()
  212 {
  213     /* main */
  214     int moreToDo;
  215 
  216     init();
  217     reset();
  218     moreToDo = getInput();
  219     while (moreToDo)
  220         {
  221             /* while */
  222             process();
  223             reset();
  224             moreToDo = getInput();
  225         } /* while */
  226 
  227     return EXIT_SUCCESS;
  228 } /* main */
  229