quetzelcoatlus

05.c

Dec 5th, 2022 (edited)
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.22 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.  
  12.     int n=0;
  13.     char **data = NULL;
  14.     char **p = NULL;
  15.  
  16.     while ((nread = getline(&line, &len, stdin)) != -1) {
  17.         if(n == 0){
  18.             n = (strlen(line)+1)/4;
  19.             data = malloc(sizeof(char*)*n);
  20.             p = malloc(sizeof(char*)*n);
  21.             for(int i=0; i<n; i++){
  22.                 data[i] = calloc(sizeof(char), BIG);
  23.                 p[i] = &(data[i][BIG/2]);
  24.             }
  25.         }
  26.         if (line[1] == '1')
  27.             break;
  28.  
  29.         for(int i=0; i<n; i++)
  30.             if(line[4*i+1] != ' ')
  31.                 *(p[i]++) = line[4*i+1];
  32.     }
  33.  
  34.     for(int i=0; i<n; i++){
  35.         while(*(p[i]-1))
  36.             --p[i];
  37.     }
  38.  
  39.     if(getline(&line, &len, stdin) != 1)
  40.         printf("WTF?");
  41.  
  42.     int a,b,c;
  43.     while(scanf("move %d from %d to %d\n", &a, &b, &c) == 3)
  44.         for(int i=0; i<a; i++)
  45.            *(--p[c-1]) = *(p[b-1]++);
  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