#include #include #include #include #include #define FL __FILE__,__LINE__ #define PD printf("."); fflush(stdout) #define tz(x) if(x){printf("\n%s:%d:5:Fail: %s is %d not zero.",FL,#x,x);}else PD; #define tde(x,y) if(x!=y){printf("\n%s:%d:6:Fail: %s is %d not %d.",FL,#x,x,y);}else PD; #define tse(x,y) if(strcmp(x,y)){printf("\n%s:%d:6:Fail: %s is %s not %s",FL,#x,x,y);}else PD; int *parseline(char *s){ int *ret=calloc(strlen(s)+1,sizeof(int)); int count =0; while(s!=NULL && *s!= '\0'){ char *n=strsep(&s," "); if(n!=NULL){ ret[count]=atoi(n); if(atoi(n)<1)abort(); count++; } } ret[count]=0; return ret; } int issafe(int *data){ if(data == NULL) return 0; int dir=0; for(int i=0;data[i]!=0;i++){ if(i>0){ if (abs(data[i]-data[i-1])>3 || data[i]==data[i-1]){ //printf(" unsafe data %d %d\n",data[i-1],data[i]); return 0; } if(dir){ if(dir<0 && data[i]>data[i-1]){ //printf(" bad dir %d %d\n",data[i-1],data[i]); return 0; }else if(dir>0 && data[i]data[i-1]){ dir=1; }else{ dir=-1; } } } return 1; } int *adup(int *a){ int len=0; for(len=0;a[len]!=0;len++); len++; int *b=calloc(len,sizeof(int)); for(int i=0; i1){ data=parseline(buf); count+=(issafe(data)); countdamp+=(issafepdamp(data)); free(data); } } printf("\nAnswer: %d\n",count); printf("\nAnswer2: %d\n",countdamp); return 0; }