From f4be6a5be3e9876bd623592366a83c567ee9f80d Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Tue, 6 Dec 2022 22:41:12 -0800 Subject: [PATCH] day 7 simplify awk solution inspired by elly's fennel solution --- day07/sol.awk | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/day07/sol.awk b/day07/sol.awk index accdf47..8469ac0 100644 --- a/day07/sol.awk +++ b/day07/sol.awk @@ -4,21 +4,10 @@ BEGIN { depth = 0 } /^\$/ { cmd = $2 } -/^\$ cd/ { - if ($3 == "/") { - depth = 0 - } else if ($3 == "..") { - depth -= 1 - } else { - cwd[depth++] = $3 - } - #print dir - next -} - -/^\$ ls$/ { - next -} +/^\$ cd \/$/ { depth = 0; next } +/^\$ cd \.\.$/ { depth -= 1; next } +/^\$ cd/ { cwd[depth++] = $3; next } +/^\$ ls$/ { next } function add(sz) { sizes["/"] += sz @@ -29,11 +18,9 @@ function add(sz) { } } -cmd == "ls" { - if ($1 != "dir") { - #print(dir "/" $2, $1) - add($1) - } +cmd == "ls" && $1 != "dir" { + #print(dir "/" $2, $1) + add($1) } END {