Advertisement
Guest User

AoC 9 Part 1

a guest
Dec 9th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.77 KB | None | 0 0
  1. //just wc the output. And subtract one, for the EOF :-)
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main(int argc, char** argv)
  6. {
  7.     char c;
  8.     while((c=getchar())!= EOF){
  9.         if(c=='('){
  10.             int len /*= readint()*/;
  11.             scanf("%d", &len);
  12.             getchar();//discard x
  13.             int i /*= readint()*/;
  14.             scanf("%d", &i);
  15.             getchar();//discard )
  16.             char* buf = malloc((len + 1) * (sizeof(char)));
  17.             if (buf == NULL) return -1;
  18.             int s = fread(buf, 1, len, stdin);
  19.             if (s != len) return -2;
  20.             buf[len]='\0';//this works, because sizeof(buf)=len+1.
  21.             for(;i>0;i--) printf("%s", buf);
  22.             free(buf);
  23.         }else{putchar(c);}
  24.     }
  25.     return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement