adventofcode2022/day07/sol.awk

60 lines
915 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/ {
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
}