diff options
Diffstat (limited to '2022/07/puzzles.sh')
-rw-r--r-- | 2022/07/puzzles.sh | 30 |
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 |