2022-12-07 19:03:14 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2022-12-07 23:51:13 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
2022-12-07 19:03:14 +01:00
|
|
|
|
|
|
|
typedef struct entry {
|
|
|
|
size_t size;
|
|
|
|
size_t n_entries;
|
2022-12-07 21:44:06 +01:00
|
|
|
size_t n_alloc;
|
2022-12-08 05:27:45 +01:00
|
|
|
size_t visit;
|
2022-12-07 21:44:06 +01:00
|
|
|
struct entry **entries;
|
2022-12-07 19:03:14 +01:00
|
|
|
struct entry *parent;
|
|
|
|
} entry;
|
|
|
|
|
2022-12-08 05:27:45 +01:00
|
|
|
entry *addent(entry *parent) {
|
2022-12-07 21:44:06 +01:00
|
|
|
if (parent->n_entries >= parent->n_alloc) {
|
2022-12-08 04:27:28 +01:00
|
|
|
parent->n_alloc += 5;
|
2022-12-07 21:44:06 +01:00
|
|
|
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++;
|
2022-12-07 19:03:14 +01:00
|
|
|
|
|
|
|
entry *e = parent->entries[parent->n_entries - 1] = calloc(1, sizeof(entry));
|
|
|
|
e->parent = parent;
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:31:12 +01:00
|
|
|
int main(int argc, char **argv) {
|
2022-12-07 23:51:13 +01:00
|
|
|
if (argc < 2) return 1;
|
|
|
|
|
2022-12-07 19:03:14 +01:00
|
|
|
entry *root = calloc(1, sizeof(entry));
|
2022-12-07 22:30:30 +01:00
|
|
|
entry *current = root;
|
2022-12-07 19:03:14 +01:00
|
|
|
size_t S = 0, G = 0, required = 0;
|
|
|
|
|
2022-12-07 23:51:13 +01:00
|
|
|
struct stat s;
|
|
|
|
if (stat(argv[1], &s) == -1) return 1;
|
|
|
|
int fd = open(argv[1], O_RDONLY);
|
|
|
|
if (fd == -1) return 1;
|
|
|
|
|
|
|
|
char *in = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
|
|
|
|
if (in == MAP_FAILED) return 1;
|
|
|
|
char *l = in;
|
|
|
|
|
2022-12-08 04:27:28 +01:00
|
|
|
while (l != in + s.st_size) {
|
|
|
|
char *nl = strchr(l, '\n');
|
|
|
|
if (l[0] == 'd') {
|
2022-12-08 05:27:45 +01:00
|
|
|
addent(current);
|
2022-12-08 04:27:28 +01:00
|
|
|
} else if (l[0] == '$') {
|
2022-12-07 22:30:30 +01:00
|
|
|
if (l[2] == 'c') {
|
2022-12-07 19:03:14 +01:00
|
|
|
if (l[5] == '/')
|
|
|
|
current = root;
|
2022-12-08 04:27:28 +01:00
|
|
|
else if (l[5] == '.') {
|
|
|
|
if (current->parent != root)
|
|
|
|
current->parent->size += current->size;
|
2022-12-07 19:03:14 +01:00
|
|
|
current = current->parent;
|
2022-12-08 05:27:45 +01:00
|
|
|
} else
|
|
|
|
current = current->entries[current->visit++];
|
2022-12-07 22:30:30 +01:00
|
|
|
}
|
2022-12-07 19:03:14 +01:00
|
|
|
} else {
|
2022-12-07 23:51:57 +01:00
|
|
|
size_t sz = 0;
|
2022-12-08 04:27:28 +01:00
|
|
|
do { sz = 10 * sz + (*l++ - '0'); } while (*l != ' ');
|
|
|
|
current->size += sz;
|
2022-12-07 19:03:14 +01:00
|
|
|
}
|
2022-12-08 04:27:28 +01:00
|
|
|
l = nl + 1;
|
2022-12-07 19:03:14 +01:00
|
|
|
}
|
|
|
|
|
2022-12-08 04:27:28 +01:00
|
|
|
for (size_t i = 0; i < root->n_entries; i++)
|
|
|
|
root->size += root->entries[i]->size;
|
|
|
|
|
2022-12-07 23:51:13 +01:00
|
|
|
if (argc == 4)
|
|
|
|
required = strtoul(argv[3], NULL, 10) - (strtoul(argv[2], NULL, 10) - root->size);
|
2022-12-07 22:31:12 +01:00
|
|
|
else
|
|
|
|
required = 30000000UL - (70000000UL - root->size);
|
2022-12-08 04:27:28 +01:00
|
|
|
|
|
|
|
current = root;
|
2022-12-07 19:03:14 +01:00
|
|
|
do {
|
|
|
|
if (current->n_entries) {
|
|
|
|
current->n_entries--;
|
|
|
|
current = (current->entries++)[0];
|
|
|
|
} else {
|
2022-12-07 22:51:26 +01:00
|
|
|
if (current->size < 100000)
|
|
|
|
S += current->size;
|
|
|
|
else if (current->size >= required && (current->size < G || !G))
|
|
|
|
G = current->size;
|
2022-12-07 19:03:14 +01:00
|
|
|
|
|
|
|
current = current->parent;
|
|
|
|
}
|
|
|
|
} while (current);
|
|
|
|
|
|
|
|
printf("Silver: %lu\nGold: %lu\n", S, G);
|
|
|
|
}
|