99 lines
2.5 KiB
C
99 lines
2.5 KiB
C
#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 {
|
|
uint32_t id;
|
|
size_t size;
|
|
size_t n_entries;
|
|
size_t n_alloc;
|
|
struct entry **entries;
|
|
struct entry *parent;
|
|
} entry;
|
|
|
|
entry *findent(entry *e, 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]->id == id)
|
|
return e->entries[i];
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
entry *addent(entry *parent, const char *name) {
|
|
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->parent = parent;
|
|
|
|
return e;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
char l[64];
|
|
entry *root = calloc(1, sizeof(entry));
|
|
entry *current = root;
|
|
size_t S = 0, G = 0, required = 0;
|
|
|
|
while (fgets(l, 64, stdin)) {
|
|
if (l[0] == '$') {
|
|
if (l[2] == 'c') {
|
|
if (l[5] == '/')
|
|
current = root;
|
|
else if (l[5] == '.')
|
|
current = current->parent;
|
|
else
|
|
current = findent(current, l + 5);
|
|
}
|
|
} else if (l[0] == 'd') {
|
|
addent(current, l + 4);
|
|
} else {
|
|
size_t sz = strtoul(l, NULL, 10);
|
|
entry *f = current;
|
|
for (; f->parent; f = f->parent)
|
|
f->size += sz;
|
|
f->size += sz;
|
|
}
|
|
}
|
|
|
|
current = root;
|
|
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->size < 100000)
|
|
S += current->size;
|
|
else if (current->size >= required && (current->size < G || !G))
|
|
G = current->size;
|
|
|
|
current = current->parent;
|
|
}
|
|
} while (current);
|
|
|
|
printf("Silver: %lu\nGold: %lu\n", S, G);
|
|
}
|