fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct list_s
  6. {
  7. long val;
  8. struct list_s *next;
  9. } list_t;
  10.  
  11. list_t * addelement (list_t * prev, long val)
  12. {
  13. list_t * el = (list_t *) malloc (sizeof (list_t));
  14. el->val = val;
  15. el->next = NULL;
  16. if (prev)
  17. prev->next = el;
  18. return el;
  19. }
  20.  
  21. list_t * populatedeck (char *line)
  22. {
  23. list_t *prev = NULL;
  24. list_t *head = NULL;
  25. size_t i;
  26. char * dup = (char *) malloc (strlen(line) + 1);
  27. strcpy(dup, line);
  28. char *tok = strtok (dup, " ");
  29. while (tok)
  30. {
  31. prev = addelement (prev, strtol (tok, NULL, 0));
  32. if (!head)
  33. head = prev;
  34. tok = strtok (NULL, " ");
  35. }
  36. return head;
  37. }
  38.  
  39. void freelist (list_t * head)
  40. {
  41. list_t *current = head;
  42. list_t *next = NULL;
  43. while (current)
  44. {
  45. next = current->next;
  46. free (current);
  47. current = next;
  48. }
  49.  
  50. }
  51.  
  52. list_t * findtail (list_t * head)
  53. {
  54. list_t *current = head;
  55. while (current && current->next) current = current->next;
  56. return current;
  57. }
  58.  
  59. list_t * addtwo(list_t * l, list_t * r) {
  60. if (!l) return r;
  61. (findtail(l)->next) = r;
  62. return l;
  63. }
  64.  
  65. int warmove (list_t ** player1, list_t ** player2)
  66. {
  67. if (!(*player1) && !(*player2)) return -1;
  68. if (!(*player1)) return 2;
  69. if (!(*player2)) return 1;
  70.  
  71. list_t *whead1 = *player1;
  72. list_t *whead2 = *player2;
  73.  
  74. list_t *current1 = whead1;
  75. list_t *current2 = whead2;
  76.  
  77. int i;
  78. for (i = 0; i < 3; i++) {
  79. if (!(current1->next) || !(current2->next)) break;
  80. current1 = current1->next;
  81. current2 = current2->next;
  82. }
  83.  
  84. (*player1) = current1->next;
  85. (*player2) = current2->next;
  86.  
  87. current1->next = NULL;
  88. current2->next = NULL;
  89.  
  90.  
  91. int winner;
  92. if (current1->val > current2->val) {winner = 1; }
  93. else if (current1->val < current2->val) { winner = 2; }
  94. else {
  95. winner = warmove (player1, player2);
  96. if (winner == -1) return -1;
  97. }
  98.  
  99. if (winner == 1) *player1 = addtwo(*player1, addtwo(whead1, whead2));
  100. if (winner == 2) *player2 = addtwo(*player2, addtwo(whead2, whead1));
  101.  
  102.  
  103. return winner;
  104. }
  105.  
  106. int normalmove (list_t ** player1, list_t ** player2)
  107. {
  108.  
  109. list_t *card1 = *player1;
  110. (*player1) = (*player1)->next;
  111. card1->next = NULL;
  112.  
  113. list_t *card2 = *player2;
  114. (*player2) = (*player2)->next;
  115. card2->next = NULL;
  116.  
  117. int winner;
  118. if (card1->val > card2->val) { winner = 1; }
  119. else if (card1->val < card2->val) { winner = 2; }
  120. else {
  121. winner = warmove (player1, player2);
  122. if (winner == -1) return 1;
  123. }
  124.  
  125.  
  126. if ( winner == 1 ) {
  127. *player1 = addtwo(*player1, addtwo(card1, card2));
  128. }
  129. if ( winner == 2 ) {
  130. *player2 = addtwo(*player2, addtwo(card2, card1));
  131. }
  132. }
  133.  
  134. void game (char *player1, char *player2)
  135. {
  136.  
  137. list_t *deck1 = populatedeck (player1);
  138. list_t *deck2 = populatedeck (player2);
  139.  
  140. while (1) {
  141. int checktie = normalmove (&deck1, &deck2);
  142. if (checktie == 1) { printf ("0\n"); break; }
  143. if (!deck1) { printf ("2\n"); break; }
  144. if (!deck2) { printf ("1\n"); break; }
  145. }
  146.  
  147. freelist (deck1);
  148. freelist (deck2);
  149. }
  150.  
  151. int main (int argv, char *argc[])
  152. {
  153. char *chal[] =
  154. { "5 1 13 10 11 3 2 10 4 12 5 11 10 5 7 6 6 11 9 6 3 13 6 1 8 1",
  155. "9 12 8 3 11 10 1 4 2 4 7 9 13 8 2 13 7 4 2 8 9 12 3 12 7 5",
  156. "3 11 6 12 2 13 5 7 10 3 10 4 12 11 1 13 12 2 1 7 10 6 12 5 8 1",
  157. "9 10 7 9 5 2 6 1 11 11 7 9 3 4 8 3 4 8 8 4 6 9 13 2 13 5",
  158. "1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13",
  159. "1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13"
  160. };
  161.  
  162. size_t i;
  163. for (i = 0; i < 3; i++) game(chal[2 * i], chal[2 * i + 1]);
  164. }
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
2
2
0