1
0
Fork 0
aoc2022/07/solution.c

113 lines
2.9 KiB
C
Raw Normal View History

2022-12-07 19:03:14 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2022-12-07 22:28:25 +01:00
#include <stdint.h>
2022-12-07 23:51:13 +01:00
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
2022-12-07 22:28:25 +01:00
uint32_t fnv1a_32(const char *buf, size_t len)
{
uint32_t h = 0x811c9dc5U;
for (; len > 0; len--)
h = 0x01000193U * (h ^ *buf++);
return h;
}
2022-12-07 19:03:14 +01:00
typedef struct entry {
2022-12-07 22:28:25 +01:00
uint32_t id;
2022-12-07 19:03:14 +01:00
size_t size;
size_t n_entries;
2022-12-07 21:44:06 +01:00
size_t n_alloc;
struct entry **entries;
2022-12-07 19:03:14 +01:00
struct entry *parent;
} entry;
entry *findent(entry *e, const char *name) {
2022-12-07 23:51:13 +01:00
uint32_t id = fnv1a_32(name, strchr(name, '\n') - name);
2022-12-07 19:03:14 +01:00
for (size_t i = 0; i < e->n_entries; i++) {
if (e->entries[i]->id == id)
return e->entries[i];
2022-12-07 19:03:14 +01:00
}
return NULL;
}
entry *addent(entry *parent, const char *name) {
2022-12-07 21:44:06 +01:00
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++;
2022-12-07 19:03:14 +01:00
entry *e = parent->entries[parent->n_entries - 1] = calloc(1, sizeof(entry));
2022-12-07 23:51:13 +01:00
e->id = fnv1a_32(name, strchr(name, '\n') - name);
2022-12-07 19:03:14 +01:00
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;
while (l - in < s.st_size) {
2022-12-07 19:03:14 +01:00
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;
else if (l[5] == '.')
current = current->parent;
else
current = findent(current, l + 5);
2022-12-07 22:30:30 +01:00
}
2022-12-07 19:03:14 +01:00
} else if (l[0] == 'd') {
addent(current, l + 4);
2022-12-07 19:03:14 +01:00
} else {
size_t sz = strtoul(l, NULL, 10);
entry *f = current;
for (; f->parent; f = f->parent)
2022-12-07 19:03:14 +01:00
f->size += sz;
f->size += sz;
}
2022-12-07 23:51:13 +01:00
l = strchr(l, '\n') + 1;
2022-12-07 19:03:14 +01:00
}
current = root;
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-07 19:03:14 +01:00
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;
2022-12-07 19:03:14 +01:00
current = current->parent;
}
} while (current);
printf("Silver: %lu\nGold: %lu\n", S, G);
}