-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve.sh
executable file
·94 lines (85 loc) · 2.36 KB
/
solve.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env bash
#
# Simple script making use of https://github.com/scarvalhojr/aoc-cli to solve the problems from terminal.
#
# TODO: create template files for part 1 and part 2.
USAGE="Enables interactive solving for one day of AoC:
1. create a ./<year>/<day>/ directory.
2. Run for part X (first 1, then 2):
a. Download the puzzle description and input.
b. Wait for an answer to try out.
c. Submit the answer.
d. If not correct, go back to 2.2. If correct, either go to part 2 or end the run.
Usage: ./solve.sh [-y <YEAR>] [-d <DAY>] [-s]
Options:
-y <YEAR> - Specifies the year to solve. Defaults to the current year.
-d <DAY> - Specifies the day to solve. Defaults to the current day.
-s - Skips parts one for the given year/day.
"
parse_cli() {
# Parse year and day from CLI arguments.
YEAR="$(date '+%Y')"
DAY="$(date '+%d')"
RUN_PART_1=true
while getopts ":y:d:sh" opt; do
case $opt in
y)
YEAR="$OPTARG"
;;
d)
DAY=$(printf '%02d' "$OPTARG")
;;
s)
RUN_PART_1=false
;;
h)
echo "$USAGE"
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
if [ -z "$YEAR" ] || [ -z "$DAY" ]; then
echo >&2 "$USAGE"
exit 1
fi
}
# Runs the loop for submitting the answers.
# Uses variables ROOT_DIR, YEAR and DAY.
solve() {
part=$1
solve_dir="$ROOT_DIR/$YEAR/$DAY"
echo ">>> Running on $solve_dir for part $part"
mkdir -p "${solve_dir}"
pushd "$solve_dir" || (echo >&2 "Cannot cd into $solve_dir" && exit 1)
aoc download -y "${YEAR}" --day "${DAY}" --overwrite
glow "$solve_dir/puzzle.md"
while true; do
date -R
read -r -p ">>> Enter your answer: " answer
date -R
echo ">>> Submitting answer..."
output=$(aoc submit -y "$YEAR" -d "$DAY" "$part" "$answer" | tee "/dev/tty")
if [[ "$output" == *"$CORRECT_TEXT"* ]]; then
echo ">>> Success: The answer is correct."
break
else
echo ">>> Error or Incorrect Answer."
fi
done
popd || (echo >&2 "Cannot cd from $solve_dir" && exit 1)
}
ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
CORRECT_TEXT="That's the right answer"
parse_cli "$@"
if [[ $RUN_PART_1 == true ]]; then
solve 1
fi
solve 2