aoc-2022/07_ONLINE_01_p2.awk

35 lines
689 B
Awk

# https://old.reddit.com/r/adventofcode/comments/zesk40/2022_day_7_solutions/iz8ppox/?context=3
{
# checked: / only occurs on first line:
if ($0 ~ /^\$ cd \//)
# root dir = #1 ; debug: name[1] = "/"
at = i = 1
else if ($0 ~ /^\$ cd \.\./)
at = up[at]
else if ($0 ~ /^\$ cd/) {
up[++i] = at
at = i # debug: name[i] = $3
}
else if ($1 ~ /^[0-9]+$/)
tot[at] += $1
}
END {
# add subdir totals
for (i in up) {
at = i
while (at != 1) {
tot[up[at]] += tot[i]
at = up[at]
}
}
PROCINFO["sorted_in"] = "@val_num_asc"
for (i in tot)
if (((70000000 - tot[1]) + tot[i]) > 30000000) {
print tot[i]
exit 0
}
}