/home/toolbox/public_html/solutions/1/170/c.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 char card[3];
28 int piles[14][8]; /* skip oth pile so that rank matches pile number */
29 char cards[15] = {" A23456789TJQK"};
30 int cardsPlayed;
31 int lastPushedDepth;
32
33 void init()
34 {
35 /* FUNCTION init */
36 int i;
37 int j;
38
39 for (i=0; 13>i; i++)
40 {
41 /* for each pile */
42 for (j=0; 8>j; j++)
43 {
44 /* for each spot */
45 piles[i][j] = 0;
46 } /* for each spot */
47 } /* for each pile */
48 cardsPlayed = 0;
49 } /* FUNCTION init */
50
51 void putCardInPile(int i, int j)
52 {
53 /* FUNCTION putCardInPile */
54 /* map cards to an integer
55 * 1 - 13 are Ace to King for Hearts
56 * 14 - 26 are Ace to King of Diamonds
57 * 27 - 39 are Ace to King of Clubs
58 * 40 - 52 are Ace to King of Spades
59 */
60 int tmp;
61 int k;
62
63 for (k=1; 14>k; k++)
64 {
65 /* hunt for card */
66 if (card[0] == cards[k])
67 {
68 tmp = k;
69 }
70 } /* hunt for card */
71 if ('D' == card[1])
72 {
73 tmp = tmp + 13;
74 }
75 if ('C' == card[1])
76 {
77 tmp = tmp + 26;
78 }
79 if ('S' == card[1])
80 {
81 tmp = tmp + 39;
82 }
83 piles[i][j] = tmp;
84 } /* FUNCTION putCardInPile */
85
86 int makeCard(int tmp)
87 {
88 /* FUNCTION makeCard */
89 card[0]=' ';
90 card[1]=' ';
91 card[2]=0;
92 if (39 < tmp)
93 {
94 /* spade */
95 card[1]='S';
96 tmp = tmp - 39;
97 } /* spade */
98 else if (26 < tmp)
99 {
100 /* spade */
101 card[1]='C';
102 tmp = tmp - 26;
103 } /* spade */
104 else if (13 < tmp)
105 {
106 /* spade */
107 card[1]='D';
108 tmp = tmp - 13;
109 } /* spade */
110 else if (0 < tmp)
111 {
112 /* heart */
113 card[1]='H';
114 } /* heart */
115 card[0]=cards[tmp];
116 } /* FUNCTION makeCard */
117
118 int rank(int num)
119 {
120 /* FUNCTION rank */
121 int tmp;
122
123 tmp = num;
124 while (13 < tmp)
125 {
126 tmp = tmp - 13;
127 }
128 return tmp;
129 } /* FUNCTION rank */
130
131 void dump()
132 {
133 /* FUNCTION dump */
134 int i;
135 int j;
136
137 for (j=0; 8>j; j++)
138 {
139 /* for each item per pile */
140 if (0 == (j % 4))
141 {
142 printf(" A 2 3 4 5 6 7 8 9 T J Q K\n");
143 }
144 printf("row %d: ", j);
145 for (i=1; 14>i; i++)
146 {
147 /* for each pile */
148 makeCard(piles[i][j]);
149 printf("%s ", card);
150 } /* for each pile */
151 printf("\n");
152 } /* for each item per pile */
153 printf("\n");
154 } /* FUNCTION dump */
155
156 int getInput()
157 {
158 /* FUNCTION getInput */
159 int dataReadFlag;
160 int i;
161 int j;
162 int k;
163
164 scanf(" %s ", card);
165 dataReadFlag = ('#' != card[0]);
166 if (dataReadFlag)
167 {
168 /* read deck */
169 i = 1;
170 j = 0;
171 putCardInPile(i, j);
172 for (k=1; 52>k; k++)
173 {
174 /* get next 51 cards */
175 scanf(" %s ", card);
176 i++;
177 if (13 < i)
178 {
179 j++;
180 i=1;
181 }
182 putCardInPile(i, j);
183 DEBUG printf("[%s] - %d (i %d) (j %d)\n", card, piles[i][j], i, j);
184 } /* get next 51 cards */
185 } /* read deck */
186 return (dataReadFlag);
187 } /* FUNCTION getInput */
188
189 int findCardInPile(int pile)
190 {
191 /* FUNCTION findCardInPile */
192 /* return 0, 1, 2, 3 indicating how deep first face down card is, -1 if none */
193 int i;
194
195 i = 0;
196 while ((4 > i) && (0 == piles[pile][i]))
197 {
198 i++;
199 }
200 if (3 < i)
201 {
202 i = -1;
203 }
204 return i;
205 } /* FUNCTION findCardInPile */
206
207 int pushCardUnderPiles(int crd)
208 {
209 /* FUNCTON pushCardUnderPiles */
210 int tmp;
211
212 tmp = rank(crd);
213 lastPushedDepth = 4;
214 while (0 != piles[tmp][lastPushedDepth])
215 {
216 lastPushedDepth++;
217 }
218 piles[tmp][lastPushedDepth] = crd;
219 return tmp;
220 } /* FUNCTON pushCardUnderPiles */
221
222 void process()
223 {
224 /* FUNCTION process */
225 int tmp;
226 int prev;
227 int depth;
228
229 dump();
230 DEBUG printf("piles[%d][%d] = %d\n", tmp, 3, piles[tmp][3]);
231 prev = 13;
232 tmp = 13;
233 depth = 0;
234 while (0 != piles[tmp][3])
235 {
236 /* keep playing cards */
237 DEBUG printf("in loop\n");
238 cardsPlayed++;
239 tmp = pushCardUnderPiles(piles[prev][depth]);
240 piles[prev][depth] = 0;
241 prev = tmp;
242 depth = findCardInPile(prev);
243 printf("(played %d) piles[%d][%d] = %d\n", cardsPlayed, tmp, depth, piles[tmp][depth]);
244 dump();
245 } /* keep playing cards */
246 DEBUG printf("put of loop\n");
247 dump();
248 makeCard(piles[tmp][7]);
249 printf("%02d,%s\n", cardsPlayed, card);
250 } /* FUNCTION process */
251
252 int main()
253 {
254 /* main */
255 int moreToDo;
256
257 init();
258 moreToDo = getInput();
259 while (moreToDo)
260 {
261 /* while */
262 process();
263 init();
264 moreToDo = getInput();
265 } /* while */
266
267 return EXIT_SUCCESS;
268 } /* main */
269