day12.c (2098B)
1 #include <stdio.h> 2 #include <stdbool.h> 3 #include "cputime.h" 4 5 #define BUF_LEN 3 6 7 int btoi(int bufl, int buf[bufl]) { 8 int num = 0; 9 for (int i=0;i<bufl;++i) num = num*10+buf[i]; 10 return num; 11 } 12 13 typedef struct { 14 char shape[3][3]; 15 } Present; 16 17 typedef struct { 18 int w; 19 int h; 20 int req[6]; 21 int req_l; 22 } Region; 23 24 int main(void) { 25 Present presents[6] = {0}; 26 int cur_present = 0; 27 bool parse_presents = true; 28 int shape_x = 0; 29 int shape_y = 0; 30 int buf[BUF_LEN]; 31 int bufl = 0; 32 Region regions[1000] = {0}; 33 int regions_l = 0; 34 for (int c=getchar();c!=EOF;c=getchar()) { 35 if (parse_presents) { 36 if (c>='0'&&c<='9') { 37 cur_present = c-'0'; 38 shape_x = 0; 39 shape_y = 0; 40 } else if (c=='.'||c=='#') { 41 presents[cur_present].shape[shape_y][shape_x++] = c; 42 } else if (c=='\n'&&shape_x>0) { 43 shape_y++; 44 shape_x = 0; 45 if (cur_present==5&&shape_y>2) parse_presents = false; 46 } 47 } else { 48 if (c>='0'&&c<='9') buf[bufl++] = c-'0'; 49 else if (c=='x') { 50 regions[regions_l].w = btoi(bufl, buf); 51 bufl = 0; 52 } else if (c==':') { 53 regions[regions_l].h = btoi(bufl, buf); 54 bufl = 0; 55 } else if ((c==' '||c=='\n')&&bufl>0) { 56 Region *reg = ®ions[regions_l]; 57 reg->req[reg->req_l++] = btoi(bufl, buf); 58 bufl = 0; 59 if (c=='\n') regions_l++; 60 } 61 } 62 } 63 64 // 7 is the rough estimate of 'condensed' shape area 65 // test input has magic value of 8. 66 int rough_area = regions_l==3 ? 8 : 7; 67 int p1 = 0; 68 for (int r=0;r<regions_l;++r) { 69 Region reg = regions[r]; 70 int min_area = reg.w*reg.h; 71 int area = 0; 72 for (int i=0;i<reg.req_l;++i) { 73 area += reg.req[i] * rough_area; 74 } 75 if (area<min_area) p1++; 76 } 77 printf("p1: %d\n", p1); 78 return 0; 79 }