From d203a6630652b15be9122e9e74389dc644fc1923 Mon Sep 17 00:00:00 2001 From: Dominique Liberda Date: Fri, 9 Dec 2022 16:08:01 +0100 Subject: [PATCH] + day 7, part 1 (finally\!) --- 07/01.sh | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 07/README.md | 21 ++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100755 07/01.sh create mode 100644 07/README.md diff --git a/07/01.sh b/07/01.sh new file mode 100755 index 0000000..9ae2987 --- /dev/null +++ b/07/01.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +pwd='' +declare -A tree + +out="$(while read line; do + IFS=' ' + if [[ "$line" == '$'* ]]; then + if [[ "$line" == *'cd'* ]]; then + fsd=($line) + if [[ ${fsd[2]} == '..' ]]; then + pwd="$(sed -E 's/\/[a-z]*$//' <<< "$pwd")" + else + pwd="$pwd/${fsd[2]}" + fi + fi + else + if [[ "$line" != "dir"* ]]; then + asd=($line) + echo "$pwd/${asd[1]} ${asd[0]}" + fi + fi +done < /tmp/07 | sed 's@//@/@g;s@//@/@g')" + +last=0 +while read line; do + item=${line/ */} + value=${line/* /} + + depth=$(($(echo "$line" | grep -o '/' | wc -l)+1)) + + IFS='/' + for (( i=1; i<$depth; i++ )); do + owo=($item) + cur=$(sed 's@ @/@g;s@$@/@' <<< "${owo[@]:0:$i}") + tree[$cur]=$((${tree[$cur]}+value)) + echo "adding from $line to $cur" + done + unset IFS + +# tree[$item]=$((${tree[$item]}+$value)) + +done <<< "$out" + +#declare -p tree +sum=0 +for i in ${!tree[@]}; do + if [[ ${tree[$i]} -le 100000 ]]; then + echo "$i ${tree[$i]}" + sum=$((sum+${tree[$i]})) + fi +done + +echo $sum + +echo ${tree[/jqfwd/hqb/vbzr/gbh/]} diff --git a/07/README.md b/07/README.md new file mode 100644 index 0000000..fa56f31 --- /dev/null +++ b/07/README.md @@ -0,0 +1,21 @@ +# day 07 (oops, only 09!) + +# part 1 + +UwU finally something **fun**! + +Writing the recursive bits weren't that hard; Debugging them +to figure out why aren't they working is another thing. I spent +a solid hour trying to debug my code completing correctly on +the sample test data, and not on my chall; The total size was +somehow *way* too small. It turned out that I've had an invalid +string replace in handling the `..` case in the recurse function. +As long as a branch never further branched out (so, one directory +continued through a path of more directories containing one or +zero directories), it was all fine. Otherwise, pwd was set to +two slashes, which broke Things. + +i hate how I handled half of the code as a subshell, but I don't +care enough about it to rewrite it now + +Well! onto part 2...