1
0
Fork 0
aoc2022/07/solution.c

122 lines
3.2 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 23:51:57 +01:00
#include <ctype.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;
2022-12-08 04:27:28 +01:00
entry *findent(entry *e, const char *name, size_t nlen) {
uint32_t id = fnv1a_32(name, nlen);
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;
}
2022-12-08 04:27:28 +01:00
entry *addent(entry *parent, const char *name, size_t nlen) {
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));
2022-12-08 04:27:28 +01:00
e->id = fnv1a_32(name, nlen);
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;
2022-12-08 04:27:28 +01:00
while (l != in + s.st_size) {
char *nl = strchr(l, '\n');
if (l[0] == 'd') {
l += 4;
addent(current, l, nl - l);
} 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 04:27:28 +01:00
} else {
l += 5;
current = findent(current, l, nl - l);
}
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 {
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);
}