aboutsummaryrefslogtreecommitdiff
path: root/2022/07/puzzles.sh
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2022-12-07 17:53:31 +0100
committerThomas Voss <mail@thomasvoss.com> 2022-12-07 17:53:31 +0100
commit4ab8876daa573ae49be0dda0507d18446cda8dba (patch)
tree291b60ac3161af0f7a6b3eafe7e6913f049de087 /2022/07/puzzles.sh
parentf17c587b737af0ae44427334dadff51dd84093f7 (diff)
Add 2022 day 7 solutions
Diffstat (limited to '2022/07/puzzles.sh')
-rw-r--r--2022/07/puzzles.sh30
1 files changed, 30 insertions, 0 deletions
diff --git a/2022/07/puzzles.sh b/2022/07/puzzles.sh
new file mode 100644
index 0000000..d09a66e
--- /dev/null
+++ b/2022/07/puzzles.sh
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+set -e
+
+ROOT="`pwd`"
+
+trap "rm -rf \"$ROOT/run\"" EXIT HUP INT TERM
+mkdir run; cd run
+
+while read -r line; do
+ case "$line" in
+ '$ cd /') cd "$ROOT/run" ;;
+ '$ cd'*) cd "${line#\$ cd }" ;;
+ dir*) mkdir "${line#dir }" ;;
+ [0-9]*) touch "${line% *}" ;;
+ esac
+done <"$ROOT/input"
+
+cd "$ROOT/run"
+find . -type d | while read -r dir; do
+ { echo -n 0; find "$dir" -type f -printf ' + %f'; } | xargs expr
+# START PART 1
+done | awk '$0 <= 100000 { s += $0 } END { print s }'
+# END PART 1 START PART 2
+done | sort -nr | awk '
+NR == 1 { n = 30000000 - (70000000 - $0); s = $0 }
+ { if ($0 < s && $0 >= n) s = $0 }
+END { print s }
+'
+# END PART 2