diff --git a/README.md b/README.md index 29904e7..b39152d 100644 --- a/README.md +++ b/README.md @@ -30,8 +30,8 @@ Generated with the [advent-of-code-rust](https://github.com/fspoettel/advent-of- ### Setup new day ```sh -# example: `./scripts/scaffold.sh 1` -./scripts/scaffold.sh +# example: `./bin/scaffold 1` +./bin/scaffold # output: # Created module "src/bin/01.rs" @@ -46,8 +46,8 @@ Every [solution](https://git.io/JyXa8) has _unit tests_ referencing the _example ### Download inputs for a day ```sh -# example: `./scripts/download.sh 1` -./scripts/download.sh +# example: `./bin/download 1` +./bin/download # output: # Loaded session cookie from "/home/felix/.adventofcode.session". @@ -58,9 +58,9 @@ Every [solution](https://git.io/JyXa8) has _unit tests_ referencing the _example # 🎄 Successfully wrote input to "src/inputs/01.txt"! ``` -Puzzle inputs are not checked into git. [See here](https://old.reddit.com/r/adventofcode/comments/k99rod/sharing_input_data_were_we_requested_not_to/gf2ukkf/?context=3) why. +To download inputs for previous years, append the `--year` flag. _(example: `./bin/download 1 --year 2020`)_ -> This script does not support downloading inputs for previous years. If you want to use this template for a previous aoc, [use the underlying `aoc-cli` binary directly](https://github.com/scarvalhojr/aoc-cli/#download-puzzle-input) or download your inputs manually. +Puzzle inputs are not checked into git. [See here](https://old.reddit.com/r/adventofcode/comments/k99rod/sharing_input_data_were_we_requested_not_to/gf2ukkf/?context=3) why. ### Run solutions for a day diff --git a/bin/download b/bin/download new file mode 100755 index 0000000..9d32f8c --- /dev/null +++ b/bin/download @@ -0,0 +1,56 @@ +#!/bin/bash + +set -e; + +if ! command -v 'aoc' &> /dev/null +then + echo "command \`aoc\` not found. Try running \`cargo install aoc-cli\` to install it." + exit 1 +fi + +POSITIONAL=() +while [[ $# -gt 0 ]]; do + key="$1" + + case $key in + -y|--year) + year="$2" + shift + shift + ;; + *) + POSITIONAL+=("$1") + shift + ;; + esac +done + +set -- "${POSITIONAL[@]}" + +if [ -z "$1" ]; then + >&2 echo "Argument is required for day." + exit 1 +fi + +day=$(echo "$1" | sed 's/^0*//'); +day_padded=$(printf %02d "$day"); + +filename="$day_padded"; +input_path="src/inputs/$filename.txt"; + +tmp_dir=$(mktemp -d); +tmp_file_path="$tmp_dir/input"; + +if [[ "$year" != "" ]] +then +aoc download --day "$day" --year "$year" --file "$tmp_file_path"; +else +aoc download --day "$day" --file "$tmp_file_path"; +fi + +cat "$tmp_file_path" > "$input_path"; +echo "---" +echo "🎄 Successfully wrote input to \"$input_path\"!" + +trap "exit 1" HUP INT PIPE QUIT TERM +trap 'rm -rf "$tmp_dir"' EXIT diff --git a/scripts/scaffold.sh b/bin/scaffold similarity index 81% rename from scripts/scaffold.sh rename to bin/scaffold index 05b2ee6..49a1e48 100755 --- a/scripts/scaffold.sh +++ b/bin/scaffold @@ -2,13 +2,13 @@ set -e; -if [ ! -n "$1" ]; then +if [ -z "$1" ]; then >&2 echo "Argument is required for day." exit 1 fi -day=$(echo $1 | sed 's/^0*//'); -day_padded=`printf %02d $day`; +day=$(echo "$1" | sed 's/^0*//'); +day_padded=$(printf %02d "$day"); filename="$day_padded"; @@ -16,9 +16,9 @@ input_path="src/inputs/$filename.txt"; example_path="src/examples/$filename.txt"; module_path="src/bin/$filename.rs"; -touch $module_path; +touch "$module_path"; -cat > $module_path < "$module_path" < u32 { 0 } @@ -51,14 +51,14 @@ mod tests { } EOF -perl -pi -e "s,DAYNUM,$day,g" $module_path; +perl -pi -e "s,DAYNUM,$day,g" "$module_path"; echo "Created module \"$module_path\""; -touch $input_path; +touch "$input_path"; echo "Created empty input file \"$input_path\""; -touch $example_path; +touch "$example_path"; echo "Created empty example file \"$example_path\""; echo "---" echo "🎄 Type \`cargo run --bin $day_padded\` to run your solution." diff --git a/scripts/download.sh b/scripts/download.sh deleted file mode 100755 index a3aefcd..0000000 --- a/scripts/download.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/bash - -set -e; - -if ! command -v 'aoc' &> /dev/null -then - echo "command \`aoc\` not found. Try running \`cargo install aoc-cli\` to install it." - exit 1 -fi - -if [ ! -n "$1" ]; then - >&2 echo "Argument is required for day." - exit 1 -fi - -day=$(echo $1 | sed 's/^0*//'); -day_padded=`printf %02d $day`; - -filename="$day_padded"; -input_path="src/inputs/$filename.txt"; - -tmp_dir=$(mktemp -d); -tmp_file_path="$tmp_dir/input"; - -aoc download --day $day --file $tmp_file_path; -cat $tmp_file_path > $input_path; -echo "---" -echo "🎄 Successfully wrote input to \"$input_path\"!" - -# Make sure it gets removed even if the script exits abnormally. -trap "exit 1" HUP INT PIPE QUIT TERM -trap 'rm -rf "$tmp_dir"' EXIT