From 4f9faaa646ba63cb4b6aa29738bbae7a6b4ff132 Mon Sep 17 00:00:00 2001 From: mhsn Date: Wed, 30 Apr 2025 21:40:41 +0100 Subject: clean up shell scripts --- bench | 52 +++++++++++++++++++++++++++++++++++----------------- check | 40 +++++++++++++++++++++++++--------------- init | 23 +++++++++++++++++------ 3 files changed, 77 insertions(+), 38 deletions(-) diff --git a/bench b/bench index 75cce29..a38b891 100755 --- a/bench +++ b/bench @@ -1,30 +1,48 @@ #!/usr/bin/env sh -year=$1 -day=$2 -lang=$3 -input=$4 +usage="usage: bench []" +year=${1?$usage} +day=${2?$usage} +lang=${3?$usage} +input=${4?$usage} warmups=${5:-0} +# get directory of this script script=$(readlink -f "$0") script_path=$(dirname "$script") -aoc_path=$script_path/$1/$2 -data_path="$aoc_path/data/$4.txt" +# find aoc and data paths +aoc_path=$script_path/$year/$day +data_path="$aoc_path/data/$input.txt" -# Check it is correct -$script_path/check $1 $2 $3 $4 >/dev/null +# check solution is correct for this input +$script_path/check $year $day $lang $input >/dev/null [ ! $? ] && echo "Incorrect solution" && exit 1 -if [ $lang = "python" ]; then - exec="python $aoc_path/python/main.py" -elif [ $lang = "rust" ]; then - # Compile first - cargo build --release --target-dir /tmp/aoc_rust --manifest-path $aoc_path/rust/Cargo.toml +case $lang in +"python") + # Sync to create venv + uv sync \ + --directory $aoc_path/python + # Bypass any `uv run` overhead and run .venv directly + exec="$aoc_path/python/.venv/bin/python $aoc_path/python/main.py" + ;; +"rust") + # Compile to /tmp/aoc_rust and run binary + cargo build \ + --release \ + --target-dir /tmp/aoc_rust \ + --manifest-path $aoc_path/rust/Cargo.toml exec="/tmp/aoc_rust/release/aoc_$year-$day" -else - echo "Unknown lang: $lang" + ;; +*) + echo "unknown lang: $lang" exit 1 -fi + ;; +esac -hyperfine --shell=none --warmup=$warmups --input=$data_path "$exec" +hyperfine \ + --shell none \ + --warmup $warmups \ + --input $data_path \ + "$exec" diff --git a/check b/check index 5fd97d7..7ba6556 100755 --- a/check +++ b/check @@ -1,24 +1,34 @@ #!/usr/bin/env sh -year=$1 -day=$2 -lang=$3 -input=$4 +usage="usage: check " +year=${1?$usage} +day=${2?$usage} +lang=${3?$usage} +input=${4?$usage} +# get directory of this script script=$(readlink -f "$0") script_path=$(dirname "$script") -aoc_path=$script_path/$1/$2 -data_path="$aoc_path/data/$4.txt" +# find aoc and data paths +aoc_path=$script_path/$year/$day +data_path="$aoc_path/data/$input.txt" -if [ $lang = "python" ]; then - got=$(cat $data_path | python $aoc_path/python/main.py) -elif [ $lang = "rust" ]; then - got=$(cat $data_path | cargo run --manifest-path $aoc_path/rust/Cargo.toml --) -else - echo "Unknown lang: $lang" +case $lang in +"python") + cmd="uv run --directory $aoc_path/python main.py" + ;; +"rust") + cmd="cargo run --manifest-path $aoc_path/rust/Cargo.toml --" + ;; +"*") + echo "unknown lang: $lang" exit 1 -fi + ;; +esac -echo "$got" | diff $aoc_path/data/$4.ans - && echo Solved wtih $lang! -echo $got +result=$(cat $data_path | $cmd) + +echo "$result" | diff $aoc_path/data/$4.ans - && echo Solved wtih $lang! +echo -e "\n---stdout---" +echo "$result" diff --git a/init b/init index b5ed7ac..2dd51ad 100755 --- a/init +++ b/init @@ -1,17 +1,19 @@ #!/usr/bin/env sh -year=$1 -day=$2 +usage="usage: init " +year=${1?$usage} +day=${2?$usage} +# get directory of this script script=$(readlink -f "$0") script_path=$(dirname "$script") -aoc_path=$script_path/$1/$2 +aoc_path=$script_path/$year/$day mkdir --parents $aoc_path +# Data directory if [ ! -d $aoc_path/data ]; then - # Data directory mkdir --parents $aoc_path/data for f in "test" "aoc"; do touch "$aoc_path/puzzle.txt" @@ -22,14 +24,23 @@ fi # Python if [ ! -d $aoc_path/python ]; then - mkdir --parents $aoc_path/python + uv init \ + --vcs none \ + --name "aoc_$year-$day" \ + --no-pin-python \ + --no-readme \ + --description "advent of code $year-$day" \ + "$aoc_path/python" cp "$script_path/template/main.py" "$aoc_path/python/main.py" echo "*" >"$aoc_path/python/.gitignore" fi # Rust if [ ! -d $aoc_path/rust ]; then - cargo new "$aoc_path/rust" --vcs none --name "aoc_$year-$day" + cargo new \ + --vcs none \ + --name "aoc_$year-$day" \ + "$aoc_path/rust" cp "$script_path/template/main.rs" "$aoc_path/rust/src/main.rs" echo "*" >"$aoc_path/rust/.gitignore" fi -- cgit v1.2.3