-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-10.c
84 lines (64 loc) · 1.9 KB
/
day-10.c
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// The header file has the input defined as a string literal.
#include "input/input-10.h"
#define PART_1_RUNS 40
#define PART_2_RUNS 50
#define PART_1_MAX_LENGTH 900000
int part_1_stack_runs(void) {
char result[PART_1_MAX_LENGTH] = INPUT;
char next[PART_1_MAX_LENGTH];
for (int i = 0; i < PART_1_RUNS; i++) {
int length = strlen(result);
int next_length = 0;
int count = 1;
for (int j = 0; j < length; j++) {
if (result[j] == result[j + 1]) {
count++;
} else {
next_length += sprintf(&next[next_length], "%d%c", count, result[j]);
count = 1;
}
}
strcpy(result, next);
}
return strlen(result);
}
int part_2_heap_runs(void) {
char input[] = INPUT;
size_t length = strlen(input);
size_t next_max_length = length * 2;
char* result = malloc(next_max_length * sizeof(char));
char* next = malloc(next_max_length * sizeof(char));
strcpy(result, input);
for (int i = 0; i < PART_2_RUNS; i++) {
length = strlen(result);
size_t next_length = 0;
int count = 1;
for (size_t j = 0; j < length; j++) {
if (result[j] == result[j + 1]) {
count++;
} else {
int needed = snprintf(NULL, 0, "%d%c", count, result[j]);
if (next_length + needed >= next_max_length) {
next_max_length = 2 * (next_max_length + needed);
next = realloc(next, next_max_length * sizeof(char));
}
next_length += sprintf(&next[next_length], "%d%c", count, result[j]);
count = 1;
}
}
strcpy(result, next);
}
int result_length = strlen(result);
free(next);
free(result);
return result_length;
}
int main(void) {
printf("Advent of Code 2015, Day 10\n");
printf("Part 1: length of result: %d\n", part_1_stack_runs());
printf("Part 2: length of result: %d\n", part_2_heap_runs());
return EXIT_SUCCESS;
}