-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·60 lines (54 loc) · 1.17 KB
/
run.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
#!/usr/bin/env bash
set -euo pipefail
# Fix shitty old sqlite version on Mac OS by including homebrew path:
PATH="/usr/local/opt/sqlite3/bin:$PATH"
function indent() {
sed 's/^/ /'
}
function run-part() {
local day="$1"
local part="$2"
local inputs=( )
if [[ ! -f "${day}/${part}.sql" ]]; then
echo "No solution available for ${day} ${part}"
return 1
fi
if [[ -f "${day}/common.sql" ]]; then
inputs=( "common.sql" )
fi
inputs=( ${inputs[@]} "${part}.sql" )
echo "$part"
pushd "$day" >& /dev/null
sqlite3 < <(
printf "CREATE TABLE input(line VARCHAR);\n.import input input\n"
cat "${inputs[@]}"
) | indent
popd >& /dev/null
}
run-day () {
local day="$1"
echo "$day"
if [[ -f "${day}/a.sql" ]]; then
run-part "$day" a | indent
fi
if [[ -f "${day}/b.sql" ]]; then
run-part "$day" b | indent
fi
}
run-all() {
for day in day*; do
if [[ ! -d "$day" ]]; then
continue
fi
run-day "$day"
done
}
case $# in
2) echo "$1"
run-part $@ | indent
;;
1) run-day $@
;;
0) run-all
;;
esac