Skip to content

Instantly share code, notes, and snippets.

@ey3ball
Last active December 7, 2022 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ey3ball/4c7ac497d3850379656297e16ef98083 to your computer and use it in GitHub Desktop.
Save ey3ball/4c7ac497d3850379656297e16ef98083 to your computer and use it in GitHub Desktop.
# Step 1 : preprocess the input
f = open("input.txt")
insts = f.readlines()
processed = []
for i in insts:
if i.startswith("$ cd /"):
processed.append("cd /home/ey3ball/projects/2022/fs/\n")
elif i.startswith("$ ls"):
pass
elif i.startswith("$ cd"):
processed.append(i.replace("$ cd", "cd"))
elif i.startswith("dir"):
processed.append(i.replace("dir", "mkdir"))
else:
processed.append("truncate -s " + i)
f = open("output.sh", "w+")
f.writelines(processed)
# Step 2 : run the script
./output.sh
# Step 3 : solve part 1
#!/bin/bash
cd fs
total=0
find . -mindepth 1 -type d | xargs -l1 du -sb | while read line; do
size=$(echo "$line" | cut -f1)
dir=$(echo "$line" | cut -f2)
adjust=$(( $(find $dir -type d | wc -l) * 4096))
actual=$(($size - $adjust))
if [ $actual -lt 100000 ]; then
total=$(( $total + $actual ))
echo $total
fi
done
Step 4 : solve part 2
$ du -sb .
45150876
$ find . -type d | wc -l
189
# 70000000 - (45150876−189×4096) = 25623268
# 30000000−25623268 = 4376732
# == target size of directory to remove
$ find . -mindepth 1 -type d | xargs -l1 du -sb | sort -n
...
4226349 ./qfrrtb/pnrdwlqn/zcj/vpqvpmv/tzr
4505354 ./pjsd/wtwhzzwz/wglhbp
5824348 ./pjsd/wtwhzzwz/dgqljsbq
...
# looks like ./pjsd/wtwhzzwz/wglhbp will work
$ find ./pjsd/wtwhzzwz/wglhbp -type d | wc -l
15
# solution = 4505354 - 15 * 4096
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment