1
0
Fork 0
aoc2022/07/solution.c

104 lines
2.7 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>
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
int isdir;
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, int isdir, const char *name) {
2022-12-07 22:28:25 +01:00
uint32_t id = fnv1a_32(name, strlen(name));
2022-12-07 19:03:14 +01:00
for (size_t i = 0; i < e->n_entries; i++) {
if (e->entries[i]->isdir == isdir)
2022-12-07 22:28:25 +01:00
if (e->entries[i]->id == id)
2022-12-07 19:03:14 +01:00
return e->entries[i];
}
return NULL;
}
entry *addent(entry *parent, int isdir, 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 22:28:25 +01:00
e->id = fnv1a_32(name, strlen(name));
2022-12-07 19:03:14 +01:00
e->isdir = isdir;
e->parent = parent;
return e;
}
int main(void) {
char l[64];
entry *root = calloc(1, sizeof(entry));
root->isdir++;
entry *current;
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[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);
} else {
char *p;
size_t sz = strtoul(l, &p, 10);
entry *f = addent(current, 0, p + 1);
f->size = sz;
for (f = current; f->parent; f = f->parent)
f->size += sz;
f->size += sz;
}
}
current = root;
required = 30000000UL - (70000000UL - root->size);
do {
if (current->n_entries) {
current->n_entries--;
current = (current->entries++)[0];
} else {
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;
}
} while (current);
printf("Silver: %lu\nGold: %lu\n", S, G);
}