summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormhsn <mail@mhsn.net>2024-12-01 12:01:00 +0000
committermhsn <mail@mhsn.net>2024-12-01 12:01:00 +0000
commit9015922392c9881ca62871692c3a6671243c1fe3 (patch)
tree3ced4980927b65675c07cfc0d8967feba85cf3b7
parent0b1a6fc65a62a656bef6055027faf2f5e1f3235a (diff)
downloadaoc-9015922392c9881ca62871692c3a6671243c1fe3.tar.gz
aoc-9015922392c9881ca62871692c3a6671243c1fe3.zip
Add util scripts
-rwxr-xr-xbench31
-rwxr-xr-xcheck25
-rwxr-xr-xinit33
-rw-r--r--template/main.py9
-rw-r--r--template/main.rs14
5 files changed, 112 insertions, 0 deletions
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(());
+}