commit ce1387ae8ecf89f0bd1d9b1a8eb4bfa6d049b4b3
parent d16acdfb603353fcf87c23784c37b043de0443c3
Author: aabacchus <ben@bvnf.space>
Date: Wed, 7 Dec 2022 15:47:17 +0000
22.7.2
Diffstat:
1 file changed, 20 insertions(+), 1 deletion(-)
diff --git a/2022/07/a.c b/2022/07/a.c
@@ -129,6 +129,21 @@ find_total_sizes_recurse(struct dir *d) {
return total;
}
+size_t
+find_smallest_at_least_recurse(struct dir *d, size_t s) {
+ size_t min = 70000000;
+ if (d == NULL)
+ return min;
+ for (size_t i = 0; i < d->n_dirs; i++) {
+ size_t x = find_smallest_at_least_recurse(d->dirs[i], s);
+ if (x < min)
+ min = x;
+ }
+ if (d->total_size >= s && d->total_size < min)
+ min = d->total_size;
+ return min;
+}
+
int
main(int argc, char **argv) {
char *buf = NULL;
@@ -208,7 +223,11 @@ main(int argc, char **argv) {
}
}
- printf("Total: %lu\n", find_total_sizes_recurse(top));
+ printf("Part A: %lu\n", find_total_sizes_recurse(top));
+
+ size_t need_to_free = top->total_size - 40000000;
+ printf("Part B: %lu\n", find_smallest_at_least_recurse(top, need_to_free));
+
destroy_dir_recurse(top);
free(buf);
fclose(f);