adventofcode2022/day07/sol.awk

47 lines
842 B
Awk
Raw Normal View History

2022-12-07 06:04:20 +00:00
# run me with awk -f sol.awk < input
BEGIN { depth = 0 }
/^\$/ { cmd = $2 }
/^\$ cd \/$/ { depth = 0; next }
/^\$ cd \.\.$/ { depth -= 1; next }
/^\$ cd/ { cwd[depth++] = $3; next }
/^\$ ls$/ { next }
2022-12-07 06:04:20 +00:00
function add(sz) {
sizes["/"] += sz
path = ""
for (i = 0; i < depth; i++) {
path = path "/" cwd[i]
sizes[path] += sz
}
}
cmd == "ls" && $1 != "dir" {
#print(dir "/" $2, $1)
add($1)
2022-12-07 06:04:20 +00:00
}
END {
print sizes["/"]
# for (i in sizes) { print i, sizes[i] }
t1 = 0
for (i in sizes) {
if (sizes[i] <= 100000) {
t1 += sizes[i]
}
}
print t1
t2 = 0
for (i in sizes) {
if (70000000 - sizes["/"] + sizes[i] >= 30000000) {
if (sizes[i] < t2 || t2 == 0)
t2 = sizes[i]
}
}
print t2
}