diff --git a/day07/sol.awk b/day07/sol.awk new file mode 100644 index 0000000..accdf47 --- /dev/null +++ b/day07/sol.awk @@ -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 +}