# 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 } 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) } 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 }