aoc-2022/07/01.sh

55 lines
961 B
Bash
Executable file

#!/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