-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.c3
119 lines (111 loc) · 2.6 KB
/
day7.c3
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
module day7;
import std::collections::list;
import std::io;
def DirList = List(<Directory*>);
struct Directory
{
String name;
DirList other_dirs;
Directory* parent_dir;
long own_files_size;
}
fn long Directory.total_size(Directory* this)
{
long total = this.own_files_size;
foreach (Directory* dir : this.other_dirs) total += dir.total_size();
return total;
}
fn Directory *handle_command(String command, Directory* top, Directory* current_dir)
{
if (command[2..] == "ls") return current_dir;
assert(command[2..3] == "cd");
String dir_name = command[5..];
switch DIR: (dir_name)
{
case "/":
return top;
case "..":
assert(current_dir.parent_dir);
return current_dir.parent_dir;
default:
foreach (Directory* dir : current_dir.other_dirs)
{
if (dir.name == dir_name)
{
return dir;
}
}
unreachable("Unknown dir");
}
}
fn void parse_dir(Directory *top)
{
File f = file::open("dir.txt", "rb")!!;
defer (void)f.close();
Directory* current_dir;
while TOP: (!f.eof())
{
String line = io::treadline(&f)!!;
if (!line.len) break;
if (line[0] == '$')
{
current_dir = handle_command(line, top, current_dir);
continue;
}
if (line[0..2] == "dir")
{
Directory *new_dir = mem::temp_new(Directory, { .name = line[4..], .parent_dir = current_dir });
current_dir.other_dirs.push(new_dir);
continue;
}
String[] split = line.tsplit(" ");
assert(split.len == 2);
current_dir.own_files_size += split[0].to_long()!!;
}
}
fn long count_dir_1(Directory *dir)
{
long total = 0;
foreach (Directory* child : dir.other_dirs)
{
total += count_dir_1(child);
}
long this_total = dir.total_size();
if (this_total < 100000) total += this_total;
return total;
}
fn void part1(Directory* top)
{
io::printfn("Total size of small dirs: %d", count_dir_1(top));
}
fn bool part2_find(Directory* dir, long *smallest_found, long needed)
{
long dir_size = dir.total_size();
if (dir_size < needed) return false;
bool found_child = false;
foreach (Directory* child : dir.other_dirs)
{
if (part2_find(child, smallest_found, needed)) found_child = true;
}
if (found_child) return true;
if (dir_size >= *smallest_found) return false;
*smallest_found = dir_size;
return true;
}
fn void part2(Directory* top)
{
long used = top.total_size();
long free = 70_000_000 - used;
long need_to_free = 30_000_000 - free;
assert(need_to_free > 0);
long smallest_found = used + 1;
part2_find(top, &smallest_found, need_to_free);
io::printfn("Smallest dir size to remove: %s", smallest_found);
}
fn void main()
{
Directory top = { .name = "/" };
parse_dir(&top);
part1(&top);
part2(&top);
}