aboutsummaryrefslogtreecommitdiff
path: root/day10/day10.c
blob: 32861987c06084461899ae07ff9a085b51b7f24e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdlib.h>

int encode(char* dest, char* src) {
  int d = 0, s = 0, num;
  char c;

  while(src[s] != '\0') {
    c = src[s];
    num = 0;

    while(src[s] == c) {
      num++;
      s++;
    }

    d += sprintf(dest + d, "%d", num);
    dest[d++] = c;
  }
  dest[d] = '\0';

  return d;
}

int main() {
  char *this, *next;
  int i, len;

  this = (char*) malloc(11 * sizeof(char));
  next = (char*) malloc(21 * sizeof(char));

  scanf("%s", this);

  for(i = 0; i < 20; i++) {
    len = encode(next, this);
    this = realloc(this, (2*len)+1);
    len = encode(this, next);
    next = realloc(next, (2*len)+1);
  }

  printf("First final length: %d\n", len);

  for(i = 0; i < 5; i++) {
    len = encode(next, this);
    this = realloc(this, (2*len)+1);
    len = encode(this, next);
    next = realloc(next, (2*len)+1);
  }

  printf("Second final length: %d\n", len);

  free(this);
  free(next);
  return 0;
}