day04.c (1216B)
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdbool.h> 4 #include "cputime.h" 5 6 typedef struct { 7 int data[16]; 8 int len; 9 } Buffer; 10 11 static int btoi(Buffer buf) { 12 int num = 0; 13 for (int i=0;i<buf.len;++i) num = num*10+buf.data[i]; 14 return num; 15 } 16 17 int main(void) { 18 Buffer from = {0}; 19 Buffer to = {0}; 20 Buffer *cur = &from; 21 for (int c=getchar();c!=EOF;c=getchar()) { 22 if (c>='0'&&c<='9') cur->data[cur->len++] = c-'0'; 23 else if (c=='-') cur = &to; 24 } 25 int ifrom = btoi(from); 26 int ito = btoi(to); 27 int p1 = 0; 28 int p2 = 0; 29 char str[7]; 30 for (int i=ifrom;i<=ito;++i) { 31 sprintf(str, "%d", i); 32 bool ok1 = false; 33 bool ok2 = false; 34 for (int j=1;j<6;++j) { 35 if (str[j]<str[j-1]) { 36 ok1 = false; 37 break; 38 } 39 if (str[j]==str[j-1]) { 40 ok1 = true; 41 if (!ok2 && !((j>1&&str[j-1]==str[j-2]) || (j<5&&str[j]==str[j+1]))) { 42 ok2 = true; 43 } 44 } 45 } 46 if (ok1) { 47 p1++; 48 if (ok2) { p2++; } 49 } 50 } 51 printf("p1: %d\np2: %d\n", p1, p2); 52 return 0; 53 }