Advertisement
quetzelcoatlus

13c.c

Dec 14th, 2022
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MAX_SIZE 400
  6.  
  7. int is_digit(const char c){
  8.     if(c>='0' && c<='9') return 1;
  9.  
  10.     return 0;
  11. }
  12.  
  13. int compare_lists(char* s1, int n1, char* s2){
  14.     int depth, k[2], u[2], ret, j;
  15.     char *p[2], *tmp, *tp[2], num[6];
  16.  
  17.     for(p[0]=s1+1,p[1]=s2+1; p[0]-s1<n1;){
  18.         if(*(p[0]) == ']'){
  19.             if(*(p[1]) == ']')
  20.                 return 0;
  21.             return -1;
  22.         } else if(*(p[1]) == ']'){
  23.             return 1;
  24.         } else if(is_digit(*(p[0])) && is_digit(*(p[1]))){
  25.             for(int i=0;i<2;i++){
  26.                 sscanf(p[i],"%d",&(k[i]));
  27.                 for(; *(p[i]) != ']' && *(p[i]) != ','; (p[i])++);
  28.             }
  29.             if(k[0]>k[1]) return 1;
  30.             if(k[0]<k[1]) return -1;
  31.         } else { //list vs list || list vs number || number vs list
  32.             for(int i=0;i<2;i++){
  33.                 if (*(p[i]) == '['){ //list
  34.                     tp[i]=p[i];
  35.                     for(tmp=tp[i]+1,depth=0; *tmp!=']' || depth!=0; tmp++) depth += *tmp=='[' ? 1 : *tmp==']' ? -1 : 0;
  36.                     u[i] = k[i] = tmp-p[i]+1;
  37.                 } else { //number
  38.                     j=0;
  39.                     num[j++]='[';
  40.                     for(tmp=p[i]; *tmp != ']' && *tmp != ','; tmp++) num[j++]=*tmp;
  41.                     num[j++]=']';
  42.                     num[j]=0;
  43.                     k[i]=j;
  44.                     u[i]=k[i]-2;
  45.                     tp[i]=num;
  46.                 }
  47.             }
  48.             if((ret = compare_lists(tp[0],k[0],tp[1])) != 0) return ret;
  49.  
  50.             for(int i=0;i<2;i++) p[i] += u[i];
  51.         }
  52.         for(int i=0;i<2;i++) if(*(p[i]) == ',') (p[i])++;
  53.     }
  54.  
  55.     return -1; //s1 ran oob -> is lesser
  56. }
  57.  
  58. void main(){
  59.     char *line = NULL, *line2 = NULL;
  60.     size_t len = 0, len2 = 0;
  61.     ssize_t nread, nread2;
  62.  
  63.     int sum=0, index=1;
  64.  
  65.     char list[MAX_SIZE][MAX_SIZE], tmp[MAX_SIZE];
  66.     int n[MAX_SIZE], k=0;
  67.  
  68.     while ((nread = getline(&line, &len, stdin)) != -1 && (nread2 = getline(&line2, &len2, stdin)) != -1) {
  69.         strcpy(list[k],line);
  70.         n[k++]=nread-1;
  71.         strcpy(list[k],line2);
  72.         n[k++]=nread2-1;
  73.  
  74.         if(compare_lists(line, nread-1, line2) <= 0) sum+=index;
  75.  
  76.         getline(&line,&len,stdin);
  77.         index++;
  78.     }
  79.  
  80.     strcpy(list[k],"[[2]]");
  81.     n[k++]=5;
  82.     strcpy(list[k],"[[6]]");
  83.     n[k++]=5;
  84.  
  85.     int tn,j;
  86.     for(int i=1;i<k;i++){ //insertion sort
  87.         strcpy(tmp,list[i]); tn=n[i];
  88.         j = i-1;
  89.         while(j >= 0 && compare_lists(list[j],n[j],tmp) > 0){
  90.             strcpy(list[j+1],list[j]); n[j+1]=n[j];
  91.             j--;
  92.         }
  93.         strcpy(list[j+1],tmp); n[j+1]=tn;
  94.     }
  95.  
  96.     int mul=1;
  97.     for(int i=0;i<k;i++){
  98.         if(!(strcmp(list[i],"[[2]]")) || !(strcmp(list[i],"[[6]]"))) mul*=i+1;
  99.         //printf("%d %d -> %d -> %s",k,i,n[i],list[i]);
  100.     }
  101.  
  102.     free(line);
  103.     free(line2);
  104.  
  105.     printf("Sum = %d\n", sum);
  106.     printf("Mul = %d\n", mul);
  107. }
Tags: adventofcode
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement