+ day 7, part 1 (finally\!)

meow
Dominique Liberda 2022-12-09 16:08:01 +01:00
parent ac61cc6a44
commit d203a66306
2 changed files with 77 additions and 0 deletions

56
07/01.sh Executable file
View File

@ -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/]}

21
07/README.md Normal file
View File

@ -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...