Skip to content

Commit b0b0cfa

Browse files
committed
Merge branch 'fix-find'
* fix-find: Fix solve_day.sh for mac Fix bash issues Get rid of bc(1) as not included in devcontainer image Fix for backfill script stats.sh: work for both GNU and BSD find
2 parents 3879d84 + 9d57b7f commit b0b0cfa

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

bin/aoc_lib.sh

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
#!/usr/bin/env bash
22
# Shared helpers for AoC scripts.
33

4+
5+
# Remove leading zeroes from a number
6+
# The 10# prefix tells bash to treat the number as base-10, which automatically removes leading zeroes.
7+
__aoc_strip_leading_zeroes() {
8+
local num="$1"
9+
echo $((10#$num))
10+
}
11+
412
# Initialize a main script for AoC.
513
aoc_init_script() {
614
aoc_cd_git_root
@@ -30,6 +38,11 @@ aoc_parse_year() {
3038
year=$(date +%Y)
3139
if [ $# -eq 1 ]; then
3240
IFS="/" read -ra ym <<< "$date"
41+
if (( ${#ym[@]} != 2 )); then
42+
echo "Could not parse year." >&2
43+
exit 7
44+
fi
45+
3346
year=${ym[0]}
3447
test ${#year} -eq 4 || year="20$year"
3548
elif [ "$(date +%m)" != 12 ]; then
@@ -46,6 +59,10 @@ aoc_parse_day() {
4659
day=$(date +%d)
4760
if [ $# -eq 1 ]; then
4861
IFS="/" read -ra ym <<< "$date"
62+
if (( ${#ym[@]} != 2 )); then
63+
echo "Could not parse date." >&2
64+
exit 9
65+
fi
4966
day=${ym[1]}
5067
test ${#day} -eq 2 || day="0$day"
5168
elif [ "$(date +%m)" != 12 ]; then
@@ -60,7 +77,7 @@ aoc_fetch_input() {
6077
local year day0 day url_fmt url
6178
year="$1"
6279
day0="$2"
63-
day=$(echo "$day0" | bc)
80+
day=$(__aoc_strip_leading_zeroes "$day0")
6481

6582
url_fmt="https://adventofcode.com/%d/day/%d/input"
6683
# shellcheck disable=SC2059
@@ -82,7 +99,7 @@ aoc_create_readme() {
8299
local year day0 day url_base url
83100
year="$1"
84101
day0="$2"
85-
day=$(echo "$day0" | bc)
102+
day=$(__aoc_strip_leading_zeroes "$day0")
86103

87104
url_base="adventofcode.com/${year}/day/${day}"
88105
url="https://${url_base}"
@@ -112,7 +129,7 @@ aoc_create_instructions_url() {
112129
local year day0 day url
113130
year="$1"
114131
day0="$2"
115-
day=$(echo "$day0" | bc)
132+
day=$(__aoc_strip_leading_zeroes "$day0")
116133

117134
url="https://adventofcode.com/${year}/day/${day}"
118135

@@ -131,7 +148,7 @@ aoc_create_input_script() {
131148
local year day0 day url
132149
year="$1"
133150
day0="$2"
134-
day=$(echo "$day0" | bc)
151+
day=$(__aoc_strip_leading_zeroes "$day0")
135152

136153
url="https://adventofcode.com/${year}/day/${day}/input"
137154

@@ -170,4 +187,4 @@ aoc_create_enter() {
170187
local path="$year/$day"
171188
mkdir -p "$path"
172189
cd "$path" || exit 6
173-
}
190+
}

bin/backfill_days.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ backfill_day() {
3030

3131

3232
backfill_days_all() {
33-
paths=$(find . -maxdepth 2 -type d -regex "\./*[0-9]+/[0-9]+" | sort | sed -e 's|./||')
33+
# Regex written to work for both GNU/BSD find meaning e.g. `+` quantifier can't be used.
34+
paths=$(find . -maxdepth 2 -type d -regex '\./20[0-9][0-9]/[0-9][0-9]*' | sort | sed -e 's|./||')
3435
for path in $paths; do
3536
cd "$path" || exit
3637

bin/solve_day.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ enter_day() {
5656

5757
local files=(input1.0 output1.0 output2.0)
5858
files+=("part1.${arg_lang}")
59-
if "$day" != 25; then
59+
if [ "$day" != 25 ]; then
6060
files+=("part2.${arg_lang}")
6161
fi
6262

@@ -94,7 +94,7 @@ day=$(aoc_parse_day "$@") || exit
9494
enter_day "$year" "$day" "$arg_lang"
9595

9696

97-
if [ "$CODESPACES" = true ]; then
97+
if [ "${CODESPACES:-}" = true ]; then
9898
test "$day" == 25 && p2="" || p2="output2.0 part2.${arg_lang}"
9999

100100
code part1."${arg_lang}" input1.0 output1.0 input "$p2"
@@ -115,4 +115,4 @@ aoc_cd_git_root # Get back again, to run git commands etc.
115115
"$SCRIPT_DIR"/stats.sh
116116
git status
117117
path="${year}/${day}"
118-
printf "\n\ngit add %s && git commit -m \"Add %s %s\" && git fetch && git rebase && git push && tig\n" "$path" "$path" "$arg_lang"
118+
printf "\n\ngit add %s && git commit -m \"Add %s %s\" && git fetch && git rebase && git push && tig\n" "$path" "$path" "$arg_lang"

bin/stats.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ nbr_solved() {
3939
local path="./${1:-}" # Unify so that path always starts with ./, so cut below works.
4040

4141
# sort -u: account for the fact that a puzzle part can be solved in multiple languages.
42-
stars=$(find "$path" -regex '.*/part[12]\.\w+$' -print | cut -d. -f2 | sort -u | wc -l)
42+
# Regex written to work for both GNU/BSD find meaning e.g. `+` quantifier can't be used.
43+
stars=$(find "$path" -iregex '.*/part[12]\.[a-z][a-z]*$' -print | cut -d. -f2 | sort -u | wc -l)
4344

4445
# Day 25 only as part1, part2 for free.
45-
stars_d25=$(find "$path" -regex '.*/25/part1\.\w+$' | cut -d. -f2 | sort -u | wc -l)
46+
stars_d25=$(find "$path" -iregex '.*/25/part1\.[a-z][a-z]*$' | cut -d. -f2 | sort -u | wc -l)
4647
((stars+=stars_d25))
4748

4849
echo "$stars"
@@ -61,4 +62,4 @@ for year in $years; do
6162
printf "%s: %s\n" "$year" "$solved"
6263
done
6364
printf "===========\n"
64-
printf "Total: ${ESC_BOLD}%d${ESC_REST}\n" "$(nbr_solved)"
65+
printf "Total: ${ESC_BOLD}%d${ESC_REST}\n" "$(nbr_solved)"

0 commit comments

Comments
 (0)