Compare commits
4 Commits
e1d55dda2e
...
327cbf6d6b
Author | SHA1 | Date |
---|---|---|
Mia Herkt | 327cbf6d6b | |
Mia Herkt | 3e5e62fe7b | |
Mia Herkt | 6060b590d3 | |
Mia Herkt | 59bf88cb6b |
|
@ -1,20 +1,32 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
uint32_t fnv1a_32(const char *buf, size_t len)
|
||||
{
|
||||
uint32_t h = 0x811c9dc5U;
|
||||
for (; len > 0; len--)
|
||||
h = 0x01000193U * (h ^ *buf++);
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
typedef struct entry {
|
||||
char *name;
|
||||
uint32_t id;
|
||||
int isdir;
|
||||
size_t size;
|
||||
struct entry **entries;
|
||||
size_t n_entries;
|
||||
size_t n_alloc;
|
||||
struct entry **entries;
|
||||
struct entry *parent;
|
||||
} entry;
|
||||
|
||||
entry *findent(entry *e, int isdir, const char *name) {
|
||||
uint32_t id = fnv1a_32(name, strlen(name));
|
||||
for (size_t i = 0; i < e->n_entries; i++) {
|
||||
if (e->entries[i]->isdir == isdir)
|
||||
if (!strcmp(e->entries[i]->name, name))
|
||||
if (e->entries[i]->id == id)
|
||||
return e->entries[i];
|
||||
}
|
||||
|
||||
|
@ -22,37 +34,41 @@ entry *findent(entry *e, int isdir, const char *name) {
|
|||
}
|
||||
|
||||
entry *addent(entry *parent, int isdir, const char *name) {
|
||||
if (parent->n_entries)
|
||||
parent->entries = realloc(parent->entries, ++(parent->n_entries) * sizeof(entry*));
|
||||
else
|
||||
parent->entries = malloc(++(parent->n_entries) * sizeof(entry*));
|
||||
if (parent->n_entries >= parent->n_alloc) {
|
||||
parent->n_alloc += 10;
|
||||
if (parent->n_entries)
|
||||
parent->entries = realloc(parent->entries, parent->n_alloc * sizeof(entry*));
|
||||
else
|
||||
parent->entries = malloc(parent->n_alloc * sizeof(entry*));
|
||||
}
|
||||
parent->n_entries++;
|
||||
|
||||
entry *e = parent->entries[parent->n_entries - 1] = calloc(1, sizeof(entry));
|
||||
e->id = fnv1a_32(name, strlen(name));
|
||||
e->isdir = isdir;
|
||||
e->name = malloc(strlen(name) + 1);
|
||||
strcpy(e->name, name);
|
||||
e->parent = parent;
|
||||
|
||||
return e;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int main(int argc, char **argv) {
|
||||
char l[64];
|
||||
entry *root = calloc(1, sizeof(entry));
|
||||
root->isdir++;
|
||||
entry *current;
|
||||
entry *current = root;
|
||||
size_t S = 0, G = 0, required = 0;
|
||||
|
||||
while (fgets(l, 64, stdin)) {
|
||||
l[strlen(l) - 1] = '\0';
|
||||
if (l[0] == '$') {
|
||||
if (l[2] == 'c')
|
||||
if (l[2] == 'c') {
|
||||
if (l[5] == '/')
|
||||
current = root;
|
||||
else if (l[5] == '.')
|
||||
current = current->parent;
|
||||
else
|
||||
current = findent(current, 1, l + 5);
|
||||
}
|
||||
} else if (l[0] == 'd') {
|
||||
if (!findent(current, 1, l + 4))
|
||||
addent(current, 1, l + 4);
|
||||
|
@ -68,17 +84,21 @@ int main(void) {
|
|||
}
|
||||
|
||||
current = root;
|
||||
required = 30000000UL - (70000000UL - root->size);
|
||||
if (argc == 3)
|
||||
required = strtoul(argv[2], NULL, 10) - (strtoul(argv[1], NULL, 10) - root->size);
|
||||
else
|
||||
required = 30000000UL - (70000000UL - root->size);
|
||||
do {
|
||||
if (current->n_entries) {
|
||||
current->n_entries--;
|
||||
current = (current->entries++)[0];
|
||||
} else {
|
||||
if (current->isdir)
|
||||
if (current->isdir) {
|
||||
if (current->size < 100000)
|
||||
S += current->size;
|
||||
else if (current->size >= required && (current->size < G || !G))
|
||||
G = current->size;
|
||||
}
|
||||
|
||||
current = current->parent;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue