Skip to content

Commit c6c0d8c

Browse files
committed
Create generate and watch scripts for 2021 in Raku
`./generate.raku dayX` creates a dayX directory, dayX.raku program, and a DayX class with template solve methods. It also creates empty input.example.txt and input.actual.txt files for AoC input. `./watch dayX` runs the dayX program on the input files when started and whenever the program or its inputs are changed. Watch saves stdout and stderr to a file in the out/ directory so it's easy to see what debug output changed between two runs.
1 parent f54401f commit c6c0d8c

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

2021/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
out/

2021/generate.raku

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env raku
2+
# Copyright 2021 Google LLC
3+
#
4+
# Use of this source code is governed by an MIT-style
5+
# license that can be found in the LICENSE file or at
6+
# https://opensource.org/licenses/MIT.
7+
8+
# Generator script for a day's Advent of Code files. Usage:
9+
# ./generate day1
10+
11+
sub MAIN(Str:D $day-dir-name) {
12+
my $day-dir = $day-dir-name.IO;
13+
my $class-name = $day-dir.basename.tc;
14+
my $day-num = $class-name.match(/\d+$/).Str;
15+
my $script = $day-dir.add($day-dir.basename ~ '.raku');
16+
$day-dir.mkdir;
17+
for <input.example.txt input.actual.txt>.map({ $day-dir.add($_) }) {
18+
$_.spurt('') unless $_.f;
19+
}
20+
$script.spurt(qq:to/END/) unless $script.f;
21+
#!/usr/bin/env raku
22+
# Copyright 2021 Google LLC
23+
#
24+
# Use of this source code is governed by an MIT-style
25+
# license that can be found in the LICENSE file or at
26+
# https://opensource.org/licenses/MIT.
27+
#
28+
# https://adventofcode.com/2021/day/$day-num
29+
30+
class $class-name \{
31+
has \$.input;
32+
33+
method solve-part1(--> Cool:D) \{
34+
return "TODO";
35+
}
36+
37+
method solve-part2(--> Cool:D) \{
38+
return "TODO";
39+
}
40+
}
41+
42+
sub MAIN(*@input-files) \{
43+
for @input-files -> \$input-file \{
44+
if (my \$input = \$input-file.IO.slurp) \{
45+
say "Running $class-name part 1 on \$input-file";
46+
my \$start1 = now;
47+
my \$solver = {$class-name}.new(:input(\$input));
48+
say \$solver.solve-part1();
49+
"Part 1 took %.3fms\\n".printf((now - \$start1)*1000);
50+
say '';
51+
say "Running $class-name part 2 on \$input-file";
52+
my \$start2 = now;
53+
\$solver = {$class-name}.new(:input(\$input));
54+
say \$solver.solve-part2();
55+
"Part 2 took %.3fms\\n".printf((now - \$start2)*1000);
56+
} else \{
57+
say "EMPTY INPUT FILE: \$input-file";
58+
}
59+
say '=' x 40;
60+
}
61+
}
62+
END
63+
$script.chmod(0o755);
64+
}

2021/watch

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/zsh
2+
# Copyright 2021 Google LLC
3+
#
4+
# Use of this source code is governed by an MIT-style
5+
# license that can be found in the LICENSE file or at
6+
# https://opensource.org/licenses/MIT.
7+
8+
# Watch an AoC directory; rerun the solver when program or input files change.
9+
# Uses https://github.com/emcrisostomo/fswatch
10+
11+
if [ $# -eq 0 ]; then
12+
echo "Usage: watch day1"
13+
exit 1
14+
fi
15+
DIR=$1
16+
PROGRAM="${DIR:a:t}.raku"
17+
DATEFMT='%Y%m%d-%H%M%S'
18+
cd $DIR
19+
mkdir -p out
20+
21+
function runscript {
22+
outfile="out/$(date +$DATEFMT).log"
23+
cmd=(raku "$PROGRAM" input.example* input.actual.txt)
24+
echo "$cmd > $outfile"
25+
echo
26+
$cmd 2>&1 | tee "$outfile"
27+
echo
28+
}
29+
30+
runscript
31+
32+
fswatch -0 -o *.raku input.* | while read -d "" event ; do
33+
runscript
34+
done

0 commit comments

Comments
 (0)