summary refs log tree commit diff homepage
path: root/2022/day07.awk
blob: ddcc7b0562ddbbe64f8d4602cdf3a38260e1e680 (plain) (blame)
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
function parent(dir) {
	p = match(dir, "/[^/]+$");
	return substr(dir, 1, p-1);
}
/\$ cd \// {
	cwd = "/";
	next;
}
/\$ cd \.\./ {
	cwd = parent(cwd);
	next;
}
/\$ cd/ {
	cwd = cwd "/" $3;
}
/^[0-9]+/ {
	for (dir = cwd; dir != ""; dir = parent(dir)) {
		dirs[dir] += $1;
	}
}
END {
	for (dir in dirs) {
		if (dirs[dir] > 100000) continue;
		sum += dirs[dir];
	}
	print sum;
	used = dirs["/"];
	unused = 70000000 - used;
	needed = 30000000 - unused;
	min = used;
	for (dir in dirs) {
		if (dirs[dir] < needed) continue;
		if (dirs[dir] < min) min = dirs[dir];
	}
	print min;
}