-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.sh
More file actions
195 lines (169 loc) · 4.9 KB
/
functions.sh
File metadata and controls
195 lines (169 loc) · 4.9 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env bash
_advent-get-description() {
local year=$(cat year.txt)
local day=$(cat day.txt)
curl \
-H "Cookie: session=$(cat session.txt)" \
-s https://adventofcode.com/${year}/day/${day} \
| jq -srR 'match("<main>.+</main>";"gm").string
| gsub(
"(?<=a href=\")(?<a>[^\"]+)\">(?<l>.+?)(?=</a)";
"\(.a)\">\(.l) [\(.a)]"
)
| gsub(
"(?<=<span title=\")(?<t>[^\"]+)\">(?<s>.+?)(?=</span)";
"\(.t)\">\(.s) [\(.t)]"
)' \
| lynx -stdin -dump -nolist > description.txt
}
advent-get-input() {
local year=${1:-$(date +'%Y')}
local day=${2:-$(date +'%d' | jq)}
curl \
-H "Cookie: session=$(cat session.txt)" \
-s https://adventofcode.com/${year}/day/${day}/input > input.txt
printf $year > year.txt
printf $day > day.txt
_advent-get-description
}
advent-part-a() {
cat > a.jq <<'TERM'
#!/bin/sh
# \
exec jq -n -R -f "$0" "$@"
[
inputs
]
TERM
chmod +x a.jq
subl a.jq
}
advent-part-b() {
_advent-get-description
cp a.jq b.jq
subl b.jq
}
advent-submit-a() {
local year=$(cat year.txt)
local day=$(cat day.txt)
curl \
-s https://adventofcode.com/${year}/day/${day}/answer \
-H "Cookie: session=$(cat session.txt)" \
-H 'content-type: application/x-www-form-urlencoded' \
-d "level=1&answer=$(./a.jq input.txt | jq -rR '@uri')" \
| jq -srR 'match("<main>.+</main>";"gm").string' | lynx -stdin -dump -nolist
}
advent-submit-b() {
local year=$(cat year.txt)
local day=$(cat day.txt)
curl \
-s https://adventofcode.com/${year}/day/${day}/answer \
-H "Cookie: session=$(cat session.txt)" \
-H 'content-type: application/x-www-form-urlencoded' \
-d "level=2&answer=$(./b.jq input.txt | jq -rR '@uri')" \
| jq -srR 'match("<main>.+</main>";"gm").string' | lynx -stdin -dump -nolist
}
advent-write-day() {
local year=$(cat year.txt)
local day=$(cat day.txt | xargs printf "%02d")
if [[ ! $day = "25" ]]; then
mv a.jq ${year}/jq/${day}-a.jq
mv b.jq ${year}/jq/${day}-b.jq
else
mv a.jq ${year}/jq/${day}.jq
fi
}
_advent_complete()
{
local cur prev
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
NEXT_DAY=($(find ./20* -name '*.jq' | jq -rR '
[ inputs | [scan("\\d+")] ]
| first(
group_by(.[0])
| map(sort_by(.[1]) | reverse[0] )
| .[] | select(.[1] != "25")
)
| .[0], (.[1] | tonumber + 1)')
)
if [[ "${prev}" == "advent-get-input" ]]; then
COMPREPLY=($(compgen -W "${NEXT_DAY[0]}" -- $cur))
return 0
fi
if [[ "${prev}" == "${NEXT_DAY[0]}" ]]; then
COMPREPLY=($(compgen -W "${NEXT_DAY[1]}" -- $cur))
return 0
fi
}
complete -F _advent_complete advent-get-input
# Local running utilities, including non commited inputs and descriptions
if [[ -d "$PWD/.tom_safe" ]] ; then
# Used to override jq version in used,
# Binaries or symlinks should be included at ~/.tom_safe/$JQ_VERSION
JQ_VERSION=${JQ_VERSION:-local}
if [[ -f "$PWD/.tom_safe/$JQ_VERSION/jq" ]] ; then
PATH="$PWD/.tom_safe/$JQ_VERSION:$PATH"
fi
x-advent-copy()
{
local year=$(cat year.txt)
local day=$(cat day.txt | xargs printf "%02d")
cp input.txt ./.tom_safe/${year}-${day}.input.txt
cp description.txt ./.tom_safe/${year}-${day}.description.txt
}
x-advent-jq()
{
local YEAR=$1
local PART=$2
if [[ $PART =~ ^[0-9]{2}$ ]] && [[ $PART != "25" ]] ; then
echo "${YEAR}/${PART}:"; echo
if [[ -f ./${YEAR}/jq/${PART}-a.jq ]]; then
time \
./${YEAR}/jq/${PART}-a.jq "${@:3}" \
./.tom_safe/${YEAR}-${PART/-[ab]/}.input.txt
echo
fi
if [[ -f ./${YEAR}/jq/${PART}-b.jq ]]; then
time \
./${YEAR}/jq/${PART}-b.jq "${@:3}" \
./.tom_safe/${YEAR}-${PART/-[ab]/}.input.txt
echo
fi
cat ./.tom_safe/${YEAR}-${PART/-[ab]/}.description.txt \
| grep -e 'answer was' | sed -Ee 's/^ +//'
echo
elif [[ $PART =~ ^[0-9]{2}-a$ ]] || [[ $PART == "25" ]] ; then
echo "${YEAR}/${PART}:"; echo
time \
./${YEAR}/jq/${PART}.jq "${@:3}" \
./.tom_safe/${YEAR}-${PART/-[ab]/}.input.txt
echo
cat ./.tom_safe/${YEAR}-${PART/-[ab]/}.description.txt \
| grep -e 'answer was' | sed -Ee 's/^ +//' | head -n 1
echo
elif [[ $PART =~ ^[0-9]{2}-b$ ]] ; then
echo "${YEAR}/${PART}:"; echo
time \
./${YEAR}/jq/${PART}.jq "${@:3}" \
./.tom_safe/${YEAR}-${PART/-[ab]/}.input.txt
echo
cat ./.tom_safe/${YEAR}-${PART/-[ab]/}.description.txt \
| grep -e 'answer was' | sed -Ee 's/^ +//' | tail -n 1
echo
else
echo "Unexpected PART: $PART"
fi
}
x-advent-go()
{
local YEAR=$1
local DAY=$2
echo "${YEAR}/${DAY}:"; echo
time ./${YEAR}/go/${DAY}.go ./.tom_safe/${YEAR}-${DAY}.input.txt
echo
cat ./.tom_safe/${YEAR}-${DAY}.description.txt \
| grep -e 'answer was' | sed -Ee 's/^ +//'
}
fi