From 4ab8876daa573ae49be0dda0507d18446cda8dba Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Wed, 7 Dec 2022 17:53:31 +0100 Subject: Add 2022 day 7 solutions --- 2022/07/puzzles.sh | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2022/07/puzzles.sh (limited to '2022/07/puzzles.sh') 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 -- cgit v1.2.3