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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#if 0
#define PART() part1(root)
#define TARGET_SIZE 100000
#else
#define PART() sz = INT_MAX; part2(root, total_size(root))
#define FILESYSTEM_SIZE 70000000
#define TARGET_SIZE 30000000
#endif
#if 0
#define FILENAME "sample.txt"
#else
#define FILENAME "input.txt"
#endif
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define INDENT_INC 2
#define ENTRIES_CAP 256
typedef enum {
DIR,
FIL,
LINK,
} type;
typedef struct node node;
node *create_node(type t, char *name, size_t sz);
void insert_node(node *parent, node* child);
node *get_dir(node *parent, char *name);
void free_tree(node *n);
void print_tree(node *n, int indent);
size_t total_size(node *n);
size_t part1(node *n);
size_t part2(node *n, size_t total);
node *root;
node *cur;
size_t sz = 0;
void parse()
{
FILE *fp = fopen(FILENAME, "r");
if(!fp) {
fprintf(stderr, "ERROR: Could not open file: %s\n", FILENAME);
exit(1);
}
char line[256];
while(fgets(line, sizeof(line), fp))
{
size_t len = strlen(line);
if(line[len-1] == '\n') line[len-1] = '\0';
char *tok0 = strtok(line, " ");
char *tok1 = strtok(NULL, " ");
char *tok2 = strtok(NULL, " ");
if(tok0[0] == '$')
{
if(strcmp(tok1, "cd") != 0) continue;
if(tok2[0] == '/') cur = root;
else cur = get_dir(cur, tok2);
continue;
}
if(strcmp(tok0, "dir") == 0)
insert_node(cur, create_node(DIR, tok1, 0));
else
insert_node(cur, create_node(FIL, tok1, atoi(tok0)));
}
fclose(fp);
}
int main(void)
{
root = create_node(DIR, "/", 0);
parse();
PART();
print_tree(root, 0);
printf("%ld\n", sz);
free_tree(root);
return 0;
}
/// ------- Definitions ------- ///
struct _dir {
size_t entries_sz;
struct node *entries[ENTRIES_CAP];
};
struct _file {
size_t sz;
};
struct _link {
struct node *to;
};
struct node {
type t;
char name[16];
union {
struct _dir dir;
struct _file file;
struct _link link;
};
};
node *_create_dot(node *n)
{
node* dot = create_node(LINK, ".", 0);
dot->link.to = n;
return dot;
}
node *_create_dotdot(node *p)
{
node *dotdot = create_node(LINK, "..", 0);
dotdot->link.to = p;
return dotdot;
}
node *create_node(type t, char *name, size_t sz)
{
node *n = malloc(sizeof(node));
n->t = t;
strncpy(n->name, name, 16);
switch(t) {
case DIR:
n->dir.entries_sz = 0;
break;
case FIL:
n->file.sz = sz;
break;
case LINK:
n->link.to = NULL;
break;
}
return n;
}
void insert_node(node *parent, node* child)
{
assert(parent->t == DIR);
assert(parent->dir.entries_sz < ENTRIES_CAP);
if(child->t == DIR) {
insert_node(child, _create_dot(child));
insert_node(child, _create_dotdot(parent));
}
parent->dir.entries[parent->dir.entries_sz++] = child;
}
node *get_dir(node *parent, char *name)
{
assert(parent->t == DIR);
struct _dir dir = parent->dir;
for(int i = 0; i < dir.entries_sz; i++)
{
node *e = dir.entries[i];
if(strcmp(e->name, name) == 0) {
if(e->t == DIR)
return e;
else if(e->t == LINK)
return e->link.to;
}
}
assert("No entries found" && 0);
}
void free_tree(node *n)
{
assert(n->t == DIR);
struct _dir dir = n->dir;
for(int i = 0; i < dir.entries_sz; i++)
{
node *e = dir.entries[i];
switch(e->t) {
case DIR:
free_tree(e);
break;
case FIL:
free(e);
break;
case LINK:
free(e);
break;
}
}
free(n);
}
void print_tree(node *n, int indent)
{
for(int i = 0; i < indent; i++)
printf(" ");
printf("- ");
switch(n->t) {
case DIR:
printf("%s (dir)\n", n->name);
for(int i = 0; i < n->dir.entries_sz; i++)
print_tree(n->dir.entries[i], indent + INDENT_INC);
break;
case FIL:
printf("%s (file, size=%ld)\n", n->name, n->file.sz);
break;
case LINK:
printf("%s (link)\n", n->name);
break;
}
}
size_t total_size(node *n)
{
size_t s = 0;
switch(n->t) {
case DIR:
for(int i = 0; i < n->dir.entries_sz; i++)
s += part1(n->dir.entries[i]);
break;
case FIL:
return n->file.sz;
case LINK:
return 0;
}
return s;
}
size_t part1(node *n)
{
size_t s = 0;
switch(n->t) {
case DIR:
for(int i = 0; i < n->dir.entries_sz; i++)
s += part1(n->dir.entries[i]);
break;
case FIL:
return n->file.sz;
case LINK:
return 0;
}
if(s <= TARGET_SIZE) sz += s;
return s;
}
size_t part2(node *n, size_t total)
{
size_t s = 0;
switch(n->t) {
case DIR:
for(int i = 0; i < n->dir.entries_sz; i++)
s += part2(n->dir.entries[i], total);
break;
case FIL:
return n->file.sz;
case LINK:
return 0;
}
if((FILESYSTEM_SIZE - total) + s >= TARGET_SIZE)
sz = MIN(sz, s);
return s;
}
|