feat: add --year flag for download script

This commit is contained in:
Felix Spöttel
2021-12-30 10:39:12 +01:00
parent b01de14927
commit c8219d41b8
4 changed files with 70 additions and 46 deletions

56
bin/download Executable file
View File

@@ -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

64
bin/scaffold Executable file
View File

@@ -0,0 +1,64 @@
#!/bin/bash
set -e;
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";
example_path="src/examples/$filename.txt";
module_path="src/bin/$filename.rs";
touch "$module_path";
cat > "$module_path" <<EOF
pub fn part_one(input: &str) -> u32 {
0
}
pub fn part_two(input: &str) -> u32 {
0
}
fn main() {
aoc::solve!(&aoc::read_file("inputs", DAYNUM), part_one, part_two)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
use aoc::read_file;
let input = read_file("examples", DAYNUM);
assert_eq!(part_one(&input), 0);
}
#[test]
fn test_part_two() {
use aoc::read_file;
let input = read_file("examples", DAYNUM);
assert_eq!(part_two(&input), 0);
}
}
EOF
perl -pi -e "s,DAYNUM,$day,g" "$module_path";
echo "Created module \"$module_path\"";
touch "$input_path";
echo "Created empty input file \"$input_path\"";
touch "$example_path";
echo "Created empty example file \"$example_path\"";
echo "---"
echo "🎄 Type \`cargo run --bin $day_padded\` to run your solution."