B1KMusic

KTaNE Module Solver: Who's On First

Sep 4th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5.  
  6. #define L1LEFT  "[# .]\n"
  7. #define L1RIGHT "[. #]\n"
  8. #define L1NONE  "[. .]\n"
  9.  
  10. char *l1res_str[6] = {
  11.     L1LEFT  L1NONE  L1NONE,
  12.     L1RIGHT L1NONE  L1NONE,
  13.     L1NONE  L1LEFT  L1NONE,
  14.     L1NONE  L1RIGHT L1NONE,
  15.     L1NONE  L1NONE  L1LEFT,
  16.     L1NONE  L1NONE  L1RIGHT
  17. };
  18.  
  19. int
  20. streq(char *s1, char *s2)
  21. {
  22.     size_t l1 = strlen(s1);
  23.     size_t l2 = strlen(s2);
  24.  
  25.     if(l1 != l2)
  26.         return 0;
  27.  
  28.     for(size_t i = 0; i < l1; i++)
  29.         if(tolower(s1[i]) != tolower(s2[i]))
  30.             return 0;
  31.  
  32.     return 1;
  33. }
  34.  
  35. int
  36. lookup_1(char *word)
  37. {
  38.     char *table[6][10] = {
  39.         {"ur", NULL},
  40.         {"first", "okay", "c", NULL},
  41.         {"yes", "nothing", "led", "they are", NULL},
  42.         {"blank", "read", "red", "you", "your", "you're", "their", NULL},
  43.         {"", "reed", "leed", "they're", NULL},
  44.         {"display", "says", "no", "lead", "hold on", "you are", "there", "see", "cee", NULL}
  45.     };
  46.  
  47.     for(size_t i = 0; i < 6; i++)
  48.         for(char **selrow = table[i]; *selrow != NULL; selrow++)
  49.             if(streq(word, *selrow))
  50.                 return i;
  51.  
  52.     return -1;
  53. }
  54.  
  55. void
  56. s2_get_input(char *out)
  57. {
  58.     int ch;
  59.  
  60.     for(size_t i = 0; i < 7; i++){
  61.         ch = getchar();
  62.  
  63.         if(isspace(ch) && ch != ' ')
  64.             break;
  65.  
  66.         *out++ = ch;
  67.     }
  68.  
  69.     *out = '\0';
  70. }
  71.  
  72. void
  73. s2_print_row(char **row)
  74. {
  75.     puts("Input the first word that matches your six face buttons\n");
  76.  
  77.     for(size_t i = 0; i < 14; i++)
  78.         printf("  %s\n", row[i]);
  79. }
  80.  
  81. void
  82. step_2(void)
  83. {
  84.     char response[8] = "";
  85.     char *table[28][15] = {
  86.         { "ready", "yes", "okay", "what", "middle", "left", "press", "right", "blank", "ready", "no", "first", "uhhh", "nothing", "wait" },
  87.         { "first", "left", "okay", "yes", "middle", "no", "right", "nothing", "uhhh", "wait", "ready", "blank", "what", "press", "first" },
  88.         { "no", "blank", "uhhh", "wait", "first", "what", "ready", "right", "yes", "nothing", "left", "press", "okay", "no", "middle" },
  89.         { "blank", "wait", "right", "okay", "middle", "blank", "press", "ready", "nothing", "no", "what", "left", "uhhh", "yes", "first" },
  90.         { "nothing", "uhhh", "right", "okay", "middle", "yes", "blank", "no", "press", "left", "what", "wait", "first", "nothing", "ready" },
  91.         { "yes", "okay", "right", "uhhh", "middle", "first", "what", "press", "ready", "nothing", "yes", "left", "blank", "no", "wait" },
  92.         { "what", "uhhh", "what", "left", "nothing", "ready", "blank", "middle", "no", "okay", "first", "wait", "yes", "press", "right" },
  93.         { "uhhh", "ready", "nothing", "left", "what", "okay", "yes", "right", "no", "press", "blank", "uhhh", "middle", "wait", "first" },
  94.         { "left", "right", "left", "first", "no", "middle", "yes", "blank", "what", "uhhh", "wait", "press", "ready", "okay", "nothing" },
  95.         { "right", "yes", "nothing", "ready", "press", "no", "wait", "what", "right", "middle", "left", "uhhh", "blank", "okay", "first" },
  96.         { "middle", "blank", "ready", "okay", "what", "nothing", "press", "no", "wait", "left", "middle", "right", "first", "uhhh", "yes" },
  97.         { "okay", "middle", "no", "first", "yes", "uhhh", "nothing", "wait", "okay", "left", "ready", "blank", "press", "what", "right" },
  98.         { "wait", "uhhh", "no", "blank", "okay", "yes", "left", "first", "press", "what", "wait", "nothing", "ready", "right", "middle" },
  99.         { "press", "right", "middle", "yes", "ready", "press", "okay", "nothing", "uhhh", "blank", "left", "first", "what", "no", "wait" },
  100.         { "you", "sure", "you are", "your", "you're", "next", "uh huh", "ur", "hold", "what?", "you", "uh uh", "like", "done", "u" },
  101.         { "you are", "your", "next", "like", "uh huh", "what?", "done", "uh uh", "hold", "you", "u", "you're", "sure", "ur", "you are" },
  102.         { "your", "uh uh", "you are", "uh huh", "your", "next", "ur", "sure", "u", "you're", "you", "what?", "hold", "like", "done" },
  103.         { "you're", "you", "you're", "ur", "next", "uh uh", "you are", "u", "your", "what?", "uh huh", "sure", "done", "like", "hold" },
  104.         { "ur", "done", "u", "ur", "uh huh", "what?", "sure", "your", "hold", "you're", "like", "next", "uh uh", "you are", "you" },
  105.         { "u", "uh huh", "sure", "next", "what?", "you're", "ur", "uh uh", "done", "u", "you", "like", "hold", "you are", "your" },
  106.         { "uh huh", "uh huh", "your", "you are", "you", "done", "hold", "uh uh", "next", "sure", "like", "you're", "ur", "u", "what?" },
  107.         { "uh uh", "ur", "u", "you are", "you're", "next", "uh uh", "done", "you", "uh huh", "like", "your", "sure", "hold", "what?" },
  108.         { "what?", "you", "hold", "you're", "your", "u", "done", "uh uh", "like", "you are", "uh huh", "ur", "next", "what?", "sure" },
  109.         { "done", "sure", "uh huh", "next", "what?", "your", "ur", "you're", "hold", "like", "you", "u", "you are", "uh uh", "done" },
  110.         { "next", "what?", "uh huh", "uh uh", "your", "hold", "sure", "next", "like", "done", "you are", "ur", "you're", "u", "you" },
  111.         { "hold", "you are", "u", "done", "uh uh", "you", "ur", "sure", "what?", "you're", "next", "hold", "uh huh", "your", "like" },
  112.         { "sure", "you are", "done", "like", "you're", "you", "hold", "uh huh", "ur", "sure", "u", "what?", "next", "your", "uh uh" },
  113.         { "like", "you're", "next", "u", "ur", "hold", "done", "uh uh", "what?", "uh huh", "you", "like", "sure", "you are", "your" }
  114.     };
  115.  
  116.     printf("Response? >");
  117.     s2_get_input(response);
  118.  
  119.     for(size_t i = 0; i < 28; i++)
  120.         if(streq(response, table[i][0])){
  121.             s2_print_row(table[i] + 1);
  122.             return;
  123.         }
  124.  
  125.     printf("No matches for '%s'. Are you sure you typed it correctly?\n", response);
  126. }
  127.  
  128. void
  129. usage(void)
  130. {
  131.     puts("Usage: whosonfirst <word on display>");
  132.     puts("Follow the prompts");
  133.     exit(1);
  134. }
  135.  
  136. int
  137. main(int argc, char **argv)
  138. {
  139.     int response;
  140.  
  141.     if(argv[1] == NULL)
  142.         usage();
  143.  
  144.     response = lookup_1(argv[1]);
  145.  
  146.     if(response == -1){
  147.         printf("Nothing for '%s'. Did you misspell it?\n", argv[1]);
  148.         return 1;
  149.     }
  150.  
  151.     printf("\"%s\" : %i\n%s", argv[1], response, l1res_str[response]);
  152.     step_2();
  153.  
  154.     return 0;
  155. }
Add Comment
Please, Sign In to add comment