-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09.c
146 lines (125 loc) · 3.46 KB
/
day09.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#define __STDC_WANT_LIB_EXT1__ 1
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *open_file(const char *filename) {
FILE *file = NULL;
errno_t res = fopen_s(&file, filename, "r");
if (res != 0) {
char message[256];
errno_t error_res = strerror_s(message, sizeof(message), res);
assert(error_res == 0);
fprintf(stderr, "Error opening file %s: %s\n", filename, message);
assert(false);
}
return file;
}
struct Array {
int size;
int capacity;
int64_t *data;
};
void array_new(struct Array *arr, int capacity) {
arr->capacity = capacity;
arr->size = 0;
arr->data = malloc(sizeof(int64_t) * capacity);
}
void array_push(struct Array *arr, int64_t value) {
if (arr->size == arr->capacity) {
arr->capacity *= 2;
arr->data = realloc(arr->data, sizeof(int64_t) * arr->capacity);
}
arr->data[arr->size] = value;
arr->size++;
}
void array_free(struct Array *arr) { free(arr->data); }
const size_t MAX_LINE_LENGTH = 50000;
int main() {
FILE *file = open_file("input");
char line[MAX_LINE_LENGTH];
struct Array filesystem;
array_new(&filesystem, 8);
int id = 0;
while (fgets(line, sizeof(line), file) != NULL) {
char *cursor = line;
bool file = true;
while (*cursor != '\n' && *cursor != '\0') {
int num = *cursor - '0';
if (file) {
for (int i = 0; i < num; i++) {
array_push(&filesystem, id);
}
id++;
} else {
for (int i = 0; i < num; i++) {
array_push(&filesystem, -1);
}
}
cursor++;
file = !file;
}
}
// for (int i = 0; i < filesystem.size; i++) {
// if (filesystem.data[i] == -1) {
// printf(".");
// } else {
// printf("%lld", filesystem.data[i]);
// }
// }
// printf("\n");
int ir = filesystem.size - 1;
while (--id >= 0) {
int size = 0;
while (ir >= 0 && filesystem.data[ir] != id) {
ir--;
}
while (ir >= 0 && filesystem.data[ir] == id) {
ir--;
size++;
}
int il = 0;
int space = 0;
while (il <= ir) {
if (filesystem.data[il] == -1) {
space++;
if (space == size) {
break;
}
} else {
space = 0;
}
il++;
}
if (space == size) {
for (int i = 0; i < size; i++) {
int il2 = il - size + i + 1;
int ir2 = ir + i + 1;
int tmp = filesystem.data[il2];
filesystem.data[il2] = filesystem.data[ir2];
filesystem.data[ir2] = tmp;
}
}
}
// for (int i = 0; i < filesystem.size; i++) {
// if (filesystem.data[i] == -1) {
// printf(".");
// } else {
// printf("%lld", filesystem.data[i]);
// }
// }
// printf("\n");
uint64_t checksum = 0;
for (uint64_t i = 0; i < filesystem.size; i++) {
uint64_t data = filesystem.data[i];
if (data != -1) {
checksum += data * i;
}
}
printf("Result: %llu\n", checksum);
fclose(file);
}