day 07 - original solution
This commit is contained in:
parent
43b5a6c6d2
commit
8fbf90abed
|
@ -0,0 +1,59 @@
|
|||
#!/bin/sh
|
||||
set -euf
|
||||
|
||||
# I highly recommend you run this in a ramfs like /tmp
|
||||
|
||||
mkdir 07_sol && cd 07_sol
|
||||
ROOT="$PWD"
|
||||
while read -r line; do
|
||||
set -- $line
|
||||
if [ "$1" = "$" ]; then
|
||||
[ "$2" = "cd" ] && shift && $@
|
||||
continue
|
||||
fi
|
||||
if [ "$1" = "dir" ]; then
|
||||
shift
|
||||
mkdir -p "$*"
|
||||
continue
|
||||
fi
|
||||
dd if=/dev/zero of="$2" bs=1 count="$1"
|
||||
done <<-EOF
|
||||
$(sed "s|cd /|cd $ROOT|g" ../07_input)
|
||||
EOF
|
||||
|
||||
cd "$ROOT"
|
||||
|
||||
# used for part 2 later
|
||||
used_space=0
|
||||
while read -r file; do
|
||||
size="$(wc -c "$file")"
|
||||
used_space=$((used_space + ${size%% *}))
|
||||
done <<-EOF
|
||||
$(find -type f)
|
||||
EOF
|
||||
needed_space=$((used_space - 40000000))
|
||||
|
||||
# I had to do the wc -c thing because the 4kb size inherent to directories got in the way
|
||||
|
||||
total_under=0
|
||||
minsize=$used_space # minimum size dir > needed space but < prev min dir
|
||||
while read -r path; do
|
||||
dirsize=0
|
||||
while read -r file; do
|
||||
size="$(wc -c "$file")"
|
||||
dirsize=$((dirsize + ${size%% *}))
|
||||
done <<-EOF
|
||||
$(find "$path" -type f)
|
||||
EOF
|
||||
if [ $dirsize -le 100000 ]; then
|
||||
total_under=$((total_under + dirsize))
|
||||
fi
|
||||
if [ $dirsize -le $minsize ] && [ $dirsize -ge $needed_space ]; then
|
||||
minsize=$dirsize
|
||||
fi
|
||||
done <<-EOF
|
||||
$(find -type d)
|
||||
EOF
|
||||
|
||||
printf "PART 1: %s\n" "$total_under"
|
||||
printf "PART 2: %s\n" "$minsize"
|
14
README.md
14
README.md
|
@ -1,14 +1,16 @@
|
|||
my advent of code 2022 solutions
|
||||
|
||||
mainly going to be using shell + posix-defined utilities, probably
|
||||
|
||||
this repo will also include online solutions that I think are neat, indicated by "ONLINE" and with credit in the file
|
||||
|
||||
all awk and shell solutions conform to POSIX unless otherwise specified. all c solutions use only the glibc standard library unless otherwise specified (and hopefully only use POSIX functions? idk).
|
||||
|
||||
## Days
|
||||
1. awk (GNU gawk)
|
||||
2. shell (POSIX)
|
||||
3. awk (POSIX)
|
||||
4. shell (POSIX)
|
||||
5. shell (POSIX; modified input, see 05_i2. could easily be adapted to not rely on it though)
|
||||
2. shell
|
||||
3. awk
|
||||
4. shell
|
||||
5. shell (modified input, see 05_i2. could easily be adapted to not rely on it though)
|
||||
- I also started work on a "general-case" C solution, but it's currently incomplete
|
||||
6. C
|
||||
7. shell
|
||||
- this solution literally emulates the entire directory structure as a whole, which I find really funny. it's probably my favorite solution
|
||||
|
|
Loading…
Reference in New Issue