-
Notifications
You must be signed in to change notification settings - Fork 0
/
day7.c
182 lines (152 loc) Β· 4.75 KB
/
day7.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// ----------------------------------------------------------------------------
// Advent of Code 2022 - Day 7
// Dale Whinham
//
// $ gcc day7.c -Wall -Wextra -Wpedantic -Werror -o day7
// $ ./day7
// ----------------------------------------------------------------------------
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INPUT_FILE "input/day7.txt"
#define TOTAL_DISK_SPACE 70000000
#define FREE_SPACE_REQUIRED 30000000
#define LARGE_DIR_THRESHOLD 100000
typedef enum
{
TYPE_FILE,
TYPE_DIRECTORY,
} dir_type_t;
typedef struct dir_node_t
{
dir_type_t type;
char name[64];
size_t size;
struct dir_node_t* parent;
struct dir_node_t* children;
size_t num_children;
} dir_node_t;
static dir_node_t root =
{
.type = TYPE_DIRECTORY,
.name = "/",
.size = 0,
.parent = NULL,
.children = NULL,
.num_children = 0,
};
static dir_node_t* find_subdir(const dir_node_t* node, const char* name)
{
for (size_t i = 0; i < node->num_children; ++i)
if (strcmp(node->children[i].name, name) == 0)
return &node->children[i];
return NULL;
}
static void add_node(dir_node_t* parent, dir_type_t type, const char* name, size_t size)
{
++parent->num_children;
parent->children = realloc(parent->children, parent->num_children * sizeof(dir_node_t));
dir_node_t* node = &parent->children[parent->num_children - 1];
node->type = type;
strcpy(node->name, name);
node->size = size;
node->parent = parent;
node->children = NULL;
node->num_children = 0;
if (type == TYPE_FILE)
do parent->size += size; while ((parent = parent->parent));
}
static void print_directory_tree(dir_node_t* node, size_t depth)
{
if (node->type == TYPE_FILE)
printf("%*s- %s (file, size=%zu)\n", (int)depth * 4, "", node->name, node->size);
else
{
printf("%*s- %s (dir)\n", (int)depth * 4, "", node->name);
for (size_t i = 0; i < node->num_children; ++i)
print_directory_tree(&node->children[i], depth + 1);
}
}
static size_t find_large_dirs(dir_node_t* node)
{
if (node->type == TYPE_FILE)
return 0;
size_t size = 0;
if (node->size <= LARGE_DIR_THRESHOLD)
size += node->size;
for (size_t i = 0; i < node->num_children; ++i)
size += find_large_dirs(&node->children[i]);
return size;
}
static dir_node_t* find_deletion_candidate(dir_node_t* node, size_t target_size)
{
if (node->type == TYPE_FILE)
return NULL;
if (node->size < target_size)
return NULL;
dir_node_t* candidate_node = node;
for (size_t i = 0; i < node->num_children; ++i)
{
dir_node_t* current_node = find_deletion_candidate(&node->children[i], target_size);
if (current_node && current_node->size < candidate_node->size)
candidate_node = current_node;
}
return candidate_node;
}
static void free_directory_tree(dir_node_t* node)
{
if (node->type == TYPE_FILE)
return;
for (size_t i = 0; i < node->num_children; ++i)
free_directory_tree(&node->children[i]);
free(node->children);
node->children = NULL;
node->num_children = 0;
}
int main()
{
FILE* f = fopen(INPUT_FILE, "r");
if (f == NULL)
return EXIT_FAILURE;
dir_node_t* current_dir = &root;
char line_buffer[512];
char name_buffer[64];
size_t file_size;
while (fgets(line_buffer, sizeof(line_buffer), f))
{
if (sscanf(line_buffer, "$ cd %s\n", name_buffer) == 1)
{
if (name_buffer[0] == '/')
current_dir = &root;
else if (name_buffer[0] == '.')
current_dir = current_dir->parent;
else
current_dir = find_subdir(current_dir, name_buffer);
}
else if (sscanf(line_buffer, "%zu %s\n", &file_size, name_buffer) == 2)
add_node(current_dir, TYPE_FILE, name_buffer, file_size);
else if (sscanf(line_buffer, "dir %s\n", name_buffer) == 1)
add_node(current_dir, TYPE_DIRECTORY, name_buffer, 0);
}
print_directory_tree(&root, 0);
size_t sum_large_dir_sizes = find_large_dirs(&root);
size_t free_space = TOTAL_DISK_SPACE - root.size;
size_t additional_space = FREE_SPACE_REQUIRED - free_space;
dir_node_t* deletion_candidate = find_deletion_candidate(&root, additional_space);
printf
(
"Free space: %zu\n"
"Sum of large directory sizes: %zu\n"
"Additional space required: %zu\n"
"Deletion candidate size: %zu\n",
free_space,
sum_large_dir_sizes,
additional_space,
deletion_candidate->size
);
free_directory_tree(&root);
fclose(f);
return EXIT_SUCCESS;
}