day 7, part 2 woohoo

meow
Dominique Liberda 2022-12-09 16:28:53 +01:00
parent d203a66306
commit 5105769cef
3 changed files with 78 additions and 3 deletions

View File

@ -52,5 +52,3 @@ for i in ${!tree[@]}; do
done
echo $sum
echo ${tree[/jqfwd/hqb/vbzr/gbh/]}

65
07/02.sh Executable file
View File

@ -0,0 +1,65 @@
#!/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
# y=$(grep -o '/' <<< "$i" | wc -l)
# if [[ "$y" == 2 ]]; then
# echo "${tree[$i]} $i"
# sum=$((sum+${tree[$i]}))
# fi
#done
#echo $sum
free=$((70000000-${tree[/]}))
echo "free: $free"
for i in ${!tree[@]}; do
if [[ $((free+${tree[$i]})) -ge 30000000 ]]; then
echo "${tree[$i]}"
fi
done | sort -n | head -n1

View File

@ -1,6 +1,6 @@
# day 07 (oops, only 09!)
# part 1
## part 1
UwU finally something **fun**!
@ -19,3 +19,15 @@ 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...
## part 2
Why the hecc did the 1st part deliberately instruct the programmer
to count directories multiple times, and the 2nd part went "well,
actually no, we want a proper size, not an imaginary size"???
thankfully, I have decided to write the parser code separate from
the summing code, so I could just easily throw the 2nd part out
while keeping the 1st part.
Anyways! that was probably the most fun chall here yet! Neat™!