summary refs log tree commit diff homepage
path: root/2022
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2022-12-07 10:22:39 -0500
committerJune McEnroe <june@causal.agency>2022-12-07 10:22:39 -0500
commit4fe185a192e8eedaf42ad660fb246940c47af26c (patch)
treee4ad53afd2524cf3fa80d6b3eac2367b44f65685 /2022
parentSolve day 6 part 2 (diff)
downloadaoc-4fe185a192e8eedaf42ad660fb246940c47af26c.tar.gz
aoc-4fe185a192e8eedaf42ad660fb246940c47af26c.zip
Solve day 7 part 1
Diffstat (limited to '2022')
-rw-r--r--2022/day07.awk27
1 files changed, 27 insertions, 0 deletions
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;
+}