From 4fe185a192e8eedaf42ad660fb246940c47af26c Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Wed, 7 Dec 2022 10:22:39 -0500 Subject: Solve day 7 part 1 --- 2022/day07.awk | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 2022/day07.awk (limited to '2022') diff --git a/2022/day07.awk b/2022/day07.awk new file mode 100644 index 0000000..4143a8b --- /dev/null +++ b/2022/day07.awk @@ -0,0 +1,27 @@ +function parent(dir) { + p = match(dir, "/[^/]+$"); + return substr(dir, 1, p-1); +} +/\$ cd \// { + cwd = "/"; + next; +} +/\$ cd \.\./ { + cwd = parent(cwd); + next; +} +/\$ cd/ { + cwd = cwd "/" $3; +} +/^[0-9]+/ { + for (dir = cwd; dir != ""; dir = parent(dir)) { + dirs[dir] += $1; + } +} +END { + for (dir in dirs) { + if (dirs[dir] > 100000) continue; + sum += dirs[dir]; + } + print sum; +} -- cgit 1.4.1