-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1.sh
executable file
·46 lines (38 loc) · 977 Bytes
/
part1.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash
file="./input.txt";
total=0;
currentDir="";
declare -A dirs;
# get all directories total sizes
while read -r line; do
if [ "$line" == "$ cd /" ]; then
currentDir="/";
elif [[ "$currentDir" != "/" && "$line" == "$ cd .." ]]; then
currentDir="${currentDir%*/*/}/";
elif [[ "$line" =~ ^\$\ cd ]]; then
currentDir+="${line:5}/";
elif [[ "$line" =~ ^\$\ ls ]]; then
dirs[$currentDir]=0;
elif [[ "$line" =~ ^dir ]]; then
# dir
# can be ignored
continue;
else
# file
# add size to directorys total size
((dirs[$currentDir]+=${line% *}));
fi
done < $file;
# sort paths (keys of $dirs)
paths=($(for path in "${!dirs[@]}"; do echo "$path"; done | sort -r));
# correct dir sizes, increment total size
total=0;
for path in "${paths[@]}"; do
dirSize=${dirs[$path]};
if [ $dirSize -le 100000 ]; then
((total+=dirSize));
fi
parent="${path%*/*/}/";
((dirs[$parent]+=dirSize));
done
echo "Answer: $total";