Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.24 KB | None | 0 0
  1.  Edit Options Buffers Tools C Help
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6.  void explode(const int code){
  7.    printf("boom!!!!\n");
  8.  }
  9.  
  10. void readInput(char buffer[], const int bufferSize){
  11.   int i;
  12.   int c;
  13.   for(i=0; i<=bufferSize; ++i){
  14.     c = getchar();
  15.  
  16.     /* this checks that you are at the end of the line */
  17.     /* Windows encodes an end-of-line as two characters: \r\n */
  18.     /* Linux just uses \n */
  19.     /* This will accept either version */
  20.     if(c == '\r'){
  21.       c = getchar();
  22.     }
  23.  
  24.     if(c == '\n'){
  25.       break;
  26.     }
  27.     else if(i<bufferSize){
  28.       buffer[i] = (char)c;
  29.     }
  30.   }
  31.  
  32. }
  33.  
  34. void part1(){
  35.   const int bufferSize = 12;
  36.   char buffer[bufferSize];
  37.   char target[] = "Airplane";
  38.   int i;
  39.  
  40.   readInput(buffer, bufferSize);
  41.  
  42.   for(i=0; i<8; i++){
  43.     if(buffer[i] != target[i]){
  44.       explode(1);
  45.     }
  46.   }
  47. }
  48.  
  49. int part2(){
  50.   int i,z;
  51.  
  52.   const int bufferSize = 12;
  53.   char buffer[bufferSize];
  54.  
  55.   readInput(buffer, bufferSize);
  56.  
  57.   /* atoi takes a string representation of a number (e.g. "1234") */
  58.   /* and converts it to an int representation of the number (e.g. 1234) */
  59.   z = atoi(buffer);
  60.   if(!z)
  61.     explode(20);
  62.  
  63.   z += 17;
  64.  
  65.   if(z != 48)
  66.     explode(21);
  67.  
  68.   return z / 2;
  69. }
  70.  
  71. void part3(){
  72.   int i,z;
  73.  
  74.   const int bufferSize = 12;
  75.   char buffer[bufferSize];
  76.  
  77.   readInput(buffer, bufferSize);
  78.  
  79.   i=0;
  80.   while(i < bufferSize && buffer[i] != 'Z' && buffer[i] != 'u'){
  81.     i++;
  82.   }
  83.  
  84.   if(i != 2)
  85.     explode(70);
  86. }
  87.  
  88. void part4(const int x){
  89.   int i,y,z;
  90.   const int bufferSize = 12;
  91.   char buffer[bufferSize];
  92.   readInput(buffer, bufferSize);
  93.  
  94.   /* atoi takes a string representation of a number (e.g. "1234") */
  95.   /* and converts it to an int representation of the number (e.g. 1234) */
  96.   z = atoi(buffer);
  97.   if(!z)
  98.     explode(20);
  99.  
  100.   y = z;
  101.   for(i=0; i<100; i++){
  102.     y += z * i * y;
  103.   }
  104.  
  105.   if(z - 12 != x){
  106.     explode(21);
  107.   }
  108. }
  109.  
  110.  
  111. void part5(char buffer[], const int bufferSize){
  112.   const char allowed[] = "a3x4sgo4pbzv";
  113.   int i, j, x;
  114.  
  115.   for(i=0; i<bufferSize; ++i){
  116.     char c;
  117.  
  118.     x = getchar();
  119.     if(x == EOF)
  120.       explode(10);
  121.     else if(x == '\n')
  122.       explode(11);
  123.  
  124.     c = (char)x;
  125.     for(j=2; j<10; ++j){
  126.       if(c == allowed[j]){
  127.         buffer[i] = c + 1;
  128.         break;
  129.       }
  130.     }
  131.     if(j == 10){
  132.       explode(12);
  133.     }
  134.  
  135.   }
  136.  
  137.   j = 0;
  138.   for(i=1; i<bufferSize; ++i){
  139.     if(buffer[i] != buffer[i-1]){
  140.       j = 1;
  141.       break;
  142.     }
  143.   }
  144.   if(j == 0)
  145.     explode(13);
  146.  
  147.   /* this checks that you are at the end of the line */
  148.   /* Windows encodes an end-of-line as two characters: \r\n */
  149.   /* Linux just uses \n */
  150.   /* This will accept either version */
  151.   x = getchar();
  152.   if(x == '\r')
  153.     x = getchar();
  154.   if(x != '\n')
  155.     explode(14);
  156. }
  157.  
  158.  
  159. void part6(const char buf1[], const int bufSize){
  160.   char buf2[bufSize];
  161.   int i, c;
  162.  
  163.   readInput(buf2, bufSize);
  164.  
  165.   for(i=0; i<bufSize; ++i){
  166.     if(buf1[i] != buf2[bufSize - 1 - i])
  167.       explode(-44);
  168.   }
  169.  
  170. }
  171.  
  172. void part7(int *x){
  173.   int y = 19;
  174.   int *z = &y;
  175.   int k;
  176.  
  177.   const int bufSize = 12;
  178.   char buffer[bufSize];
  179.  
  180.   *z -= 3;
  181. readInput(buffer, bufSize);
  182.  
  183.   /* atoi takes a string representation of a number (e.g. "1234") */
  184.   /* and converts it to an int representation of the number (e.g. 1234) */
  185.   k = atoi(buffer);
  186.  
  187.   if(k + *x != y)
  188.     explode(70);
  189.  
  190. }
  191.  
  192. void theBomb(){
  193.   const int bufSize = 10;
  194.   char buffer[bufSize];
  195.   int result;
  196.  
  197.   printf("Enter the stage 1 password: ");
  198.   part1();
  199.   printf("Stage 1 defused.\n");
  200.  
  201.   printf("Enter the stage 2 password: ");
  202.   result = part2();
  203.   printf("Stage 2 defused.\n");
  204.  
  205.   printf("Enter the stage 3 password: ");
  206.   part3();
  207.   printf("Stage 3 defused.\n");
  208.  
  209.   printf("Enter the stage 4 password: ");
  210.   part4(result);
  211.  printf("Stage 4 defused.\n");
  212.  
  213.   printf("Enter the stage 5 password: ");
  214.   part5(buffer, bufSize);
  215.   printf("Stage 5 defused.\n");
  216.  
  217.   printf("Enter the stage 6 password: ");
  218.   part6(buffer, bufSize);
  219.   printf("Stage 6 defused.\n");
  220.  
  221.   printf("Enter the stage 7 password: ");
  222.   part7(&result);
  223.   printf("Stage 7 defused.\n");
  224.  
  225.   printf("The entire bomb has been defused.\n");
  226. }
  227.  
  228. int main(){
  229.   theBomb();
  230.   return 1;
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement