From 9015922392c9881ca62871692c3a6671243c1fe3 Mon Sep 17 00:00:00 2001 From: mhsn Date: Sun, 1 Dec 2024 12:01:00 +0000 Subject: Add util scripts --- bench | 31 +++++++++++++++++++++++++++++++ check | 25 +++++++++++++++++++++++++ init | 33 +++++++++++++++++++++++++++++++++ template/main.py | 9 +++++++++ template/main.rs | 14 ++++++++++++++ 5 files changed, 112 insertions(+) create mode 100755 bench create mode 100755 check create mode 100755 init create mode 100644 template/main.py create mode 100644 template/main.rs diff --git a/bench b/bench new file mode 100755 index 0000000..e6a710d --- /dev/null +++ b/bench @@ -0,0 +1,31 @@ +#!/usr/bin/env zsh + +year=$1 +day=$2 +lang=$3 +input=$4 +warmups=${5:=0} + +script=$(readlink -f "$0") +script_path=$(dirname "$script") + +aoc_path=$script_path/$1/$2 +data_path="$aoc_path/data/$4.txt" + +# Check it is correct +$script_path/check $1 $2 $3 $4 > /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 + exec="/tmp/aoc_rust/release/aoc_$year-$day" +else + echo "Unknown lang: $lang" + exit 1 +fi + +hyperfine --shell=none --warmup=$warmups --input=$data_path $exec diff --git a/check b/check new file mode 100755 index 0000000..06f444b --- /dev/null +++ b/check @@ -0,0 +1,25 @@ +#!/usr/bin/env zsh + +year=$1 +day=$2 +lang=$3 +input=$4 + + +script=$(readlink -f "$0") +script_path=$(dirname "$script") + +aoc_path=$script_path/$1/$2 +data_path="$aoc_path/data/$4.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 -- <(cat $data_path)) +else + echo "Unknown lang: $lang" + exit 1 +fi + +diff <(echo $got) "$aoc_path/data/$4.ans" && echo "Solved with $lang!" +echo $got diff --git a/init b/init new file mode 100755 index 0000000..422d762 --- /dev/null +++ b/init @@ -0,0 +1,33 @@ +#!/usr/bin/env zsh + +year=$1 +day=$2 + +script=$(readlink -f "$0") +script_path=$(dirname "$script") + +aoc_path=$script_path/$1/$2 + +mkdir --parents $aoc_path + +if [[ ! -d $aoc_path/data ]]; then + # Data directory + mkdir --parents $aoc_path/data + for f in "test" "aoc"; do + touch "$aoc_path/puzzle.txt" + touch "$aoc_path/data/$f.txt" + echo "silver: ???\ngold: ???" > "$aoc_path/data/$f.ans" + done +fi + +# Python +if [[ ! -d $aoc_path/python ]]; then + mkdir --parents $aoc_path/python + cp "$script_path/template/main.py" "$aoc_path/python/main.py" +fi + +# Rust +if [[ ! -d $aoc_path/rust ]]; then + cargo new "$aoc_path/rust" --vcs none --name "aoc_$year-$day" + cp "$script_path/template/main.rs" "$aoc_path/rust/src/main.rs" +fi diff --git a/template/main.py b/template/main.py new file mode 100644 index 0000000..9f1d48e --- /dev/null +++ b/template/main.py @@ -0,0 +1,9 @@ +from fileinput import input + +lines = [line.strip() for line in input()] + +silver = 0 +gold = 0 + +print("silver:", silver) +print("gold:", gold) diff --git a/template/main.rs b/template/main.rs new file mode 100644 index 0000000..88bdc79 --- /dev/null +++ b/template/main.rs @@ -0,0 +1,14 @@ +use std::io; + +fn main() -> io::Result<()> { + for line in io::stdin().lines() { + let _line = line?; + } + + let silver: u64 = 0; + let gold: u64 = 0; + println!("silver: {silver}"); + println!("gold: {gold}"); + + return Ok(()); +} -- cgit v1.2.3