quetzelcoatlus

05b.c

Dec 5th, 2022 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void main(){
  6.     char *line = NULL;
  7.     size_t len = 0;
  8.     ssize_t nread;
  9.  
  10.     int BIG = 256;
  11.     int n=0;
  12.     char **data = NULL;
  13.     char **p = NULL;
  14.  
  15.     while ((nread = getline(&line, &len, stdin)) != -1) {
  16.         if(n == 0){
  17.             n = (strlen(line)+1)/4;
  18.             data = malloc(sizeof(char*)*n);
  19.             p = malloc(sizeof(char*)*n);
  20.             for(int i=0; i<n; i++){
  21.                 data[i] = calloc(sizeof(char), BIG);
  22.                 p[i] = &(data[i][BIG/2]);
  23.             }
  24.         }
  25.         if (line[1] == '1')
  26.             break;
  27.  
  28.         for(int i=0; i<n; i++)
  29.             if(line[4*i+1] != ' ')
  30.                 *(p[i]++) = line[4*i+1];
  31.     }
  32.  
  33.     for(int i=0; i<n; i++){
  34.         while(*(p[i]-1))
  35.             --p[i];
  36.     }
  37.  
  38.     if(getline(&line, &len, stdin) != 1)
  39.         printf("WTF?");
  40.  
  41.     int a,b,c;
  42.     while(scanf("move %d from %d to %d\n", &a, &b, &c) == 3){
  43.         for(int i=a; i>0; i--)
  44.            *(p[c-1]-i) = *(p[b-1]++);
  45.         p[c-1] = p[c-1]-a;
  46.     }
  47.  
  48.     for(int i=0; i<n; i++){
  49.         printf("%c", *(p[i]));
  50.     }
  51.  
  52.     for(int i=0; i<n; i++){
  53.         free(data[i]);
  54.     }
  55.     free(data);
  56.     free(p);
  57.     free(line);
  58. }
Tags: adventofcode
Add Comment
Please, Sign In to add comment