-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextractexamples
More file actions
executable file
·61 lines (58 loc) · 1.51 KB
/
extractexamples
File metadata and controls
executable file
·61 lines (58 loc) · 1.51 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
#!/bin/zsh
# Copyright 2025 Trevor Stone
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.
# Find sample input data (in <pre><code> tags) in a puzzle HTML file and save
# them to input.example.txt, input.example2.txt, etc. If the sample input is
# already in any input.example* then it's not saved a second time.
if (( $# < 1 )); then
print -u 2 "Usage: $0 day1 day1/puzzle/"
exit 1
fi
daydir=$1
puzzle=${2-$daydir/puzzle/puzzle.html}
((exnum=0))
read -r -d '' awkscript <<ENDAWK
/<pre><code>/, /<\/code><\/pre>/ {
sub("<pre><code>", "");
sub("</code></pre>", "\0");
gsub("<", "<");
gsub(">", ">");
gsub("&", "&");
print;
}
ENDAWK
awk "$awkscript" $puzzle \
| while read -r -d '' ex ; do
((exnum++))
print -u 2 -f "Example %d is\n\e[1m%s\e[0m\n" $exnum $ex
((doit=1))
foreach existing ($daydir/input.example*.txt) do
if diff -s -q $existing <(<<<$ex) > /dev/null ; then
print -u 2 "Example $exnum is already $existing"
((doit=0))
fi
done
if (($doit)); then
if read -q "?Save example $exnum? " ; then
else
((doit=0))
fi
print -u 2
fi
if (($doit)); then
exfile=$daydir/input.example.txt
if [[ -s $exfile ]]; then
for (( i=2 ; $i ; i++ )) do
exfile=$daydir/input.example$i.txt
if [[ ! -s $exfile ]]; then
break
fi
done
fi
print -u 2 "Writing example $exnum to $exfile"
cat <<< $ex > $exfile
fi
done