day 7 awk solution

main
magical 2022-12-06 22:04:20 -08:00
parent 0a590f5d36
commit e2a67c4b5c
1 changed files with 59 additions and 0 deletions

59
day07/sol.awk 100644
View File

@ -0,0 +1,59 @@
# run me with awk -f sol.awk < input
BEGIN { depth = 0 }
/^\$/ { cmd = $2 }
/^\$ cd/ {
if ($3 == "/") {
depth = 0
} else if ($3 == "..") {
depth -= 1
} else {
cwd[depth++] = $3
}
#print dir
next
}
/^\$ ls$/ {
next
}
function add(sz) {
sizes["/"] += sz
path = ""
for (i = 0; i < depth; i++) {
path = path "/" cwd[i]
sizes[path] += sz
}
}
cmd == "ls" {
if ($1 != "dir") {
#print(dir "/" $2, $1)
add($1)
}
}
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
}