Skip to content

Commit c8219d4

Browse files
committed
feat: add --year flag for download script
1 parent b01de14 commit c8219d4

File tree

4 files changed

+70
-46
lines changed

4 files changed

+70
-46
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ Generated with the [advent-of-code-rust](https://github.com/fspoettel/advent-of-
3030
### Setup new day
3131

3232
```sh
33-
# example: `./scripts/scaffold.sh 1`
34-
./scripts/scaffold.sh <day>
33+
# example: `./bin/scaffold 1`
34+
./bin/scaffold <day>
3535

3636
# output:
3737
# Created module "src/bin/01.rs"
@@ -46,8 +46,8 @@ Every [solution](https://git.io/JyXa8) has _unit tests_ referencing the _example
4646
### Download inputs for a day
4747

4848
```sh
49-
# example: `./scripts/download.sh 1`
50-
./scripts/download.sh <day>
49+
# example: `./bin/download 1`
50+
./bin/download <day>
5151

5252
# output:
5353
# Loaded session cookie from "/home/felix/.adventofcode.session".
@@ -58,9 +58,9 @@ Every [solution](https://git.io/JyXa8) has _unit tests_ referencing the _example
5858
# 🎄 Successfully wrote input to "src/inputs/01.txt"!
5959
```
6060

61-
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.
61+
To download inputs for previous years, append the `--year` flag. _(example: `./bin/download 1 --year 2020`)_
6262

63-
> 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.
63+
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.
6464

6565
### Run solutions for a day
6666

bin/download

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
3+
set -e;
4+
5+
if ! command -v 'aoc' &> /dev/null
6+
then
7+
echo "command \`aoc\` not found. Try running \`cargo install aoc-cli\` to install it."
8+
exit 1
9+
fi
10+
11+
POSITIONAL=()
12+
while [[ $# -gt 0 ]]; do
13+
key="$1"
14+
15+
case $key in
16+
-y|--year)
17+
year="$2"
18+
shift
19+
shift
20+
;;
21+
*)
22+
POSITIONAL+=("$1")
23+
shift
24+
;;
25+
esac
26+
done
27+
28+
set -- "${POSITIONAL[@]}"
29+
30+
if [ -z "$1" ]; then
31+
>&2 echo "Argument is required for day."
32+
exit 1
33+
fi
34+
35+
day=$(echo "$1" | sed 's/^0*//');
36+
day_padded=$(printf %02d "$day");
37+
38+
filename="$day_padded";
39+
input_path="src/inputs/$filename.txt";
40+
41+
tmp_dir=$(mktemp -d);
42+
tmp_file_path="$tmp_dir/input";
43+
44+
if [[ "$year" != "" ]]
45+
then
46+
aoc download --day "$day" --year "$year" --file "$tmp_file_path";
47+
else
48+
aoc download --day "$day" --file "$tmp_file_path";
49+
fi
50+
51+
cat "$tmp_file_path" > "$input_path";
52+
echo "---"
53+
echo "🎄 Successfully wrote input to \"$input_path\"!"
54+
55+
trap "exit 1" HUP INT PIPE QUIT TERM
56+
trap 'rm -rf "$tmp_dir"' EXIT

scripts/scaffold.sh renamed to bin/scaffold

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22

33
set -e;
44

5-
if [ ! -n "$1" ]; then
5+
if [ -z "$1" ]; then
66
>&2 echo "Argument is required for day."
77
exit 1
88
fi
99

10-
day=$(echo $1 | sed 's/^0*//');
11-
day_padded=`printf %02d $day`;
10+
day=$(echo "$1" | sed 's/^0*//');
11+
day_padded=$(printf %02d "$day");
1212

1313
filename="$day_padded";
1414

1515
input_path="src/inputs/$filename.txt";
1616
example_path="src/examples/$filename.txt";
1717
module_path="src/bin/$filename.rs";
1818

19-
touch $module_path;
19+
touch "$module_path";
2020

21-
cat > $module_path <<EOF
21+
cat > "$module_path" <<EOF
2222
pub fn part_one(input: &str) -> u32 {
2323
0
2424
}
@@ -51,14 +51,14 @@ mod tests {
5151
}
5252
EOF
5353

54-
perl -pi -e "s,DAYNUM,$day,g" $module_path;
54+
perl -pi -e "s,DAYNUM,$day,g" "$module_path";
5555

5656
echo "Created module \"$module_path\"";
5757

58-
touch $input_path;
58+
touch "$input_path";
5959
echo "Created empty input file \"$input_path\"";
6060

61-
touch $example_path;
61+
touch "$example_path";
6262
echo "Created empty example file \"$example_path\"";
6363
echo "---"
6464
echo "🎄 Type \`cargo run --bin $day_padded\` to run your solution."

scripts/download.sh

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)