Skip to content

Commit a1263e1

Browse files
committed
Separate template file, compare expected value, better watch setup
1 parent c6c0d8c commit a1263e1

File tree

4 files changed

+118
-64
lines changed

4 files changed

+118
-64
lines changed

2021/generate.raku

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,51 +14,15 @@ sub MAIN(Str:D $day-dir-name) {
1414
my $day-num = $class-name.match(/\d+$/).Str;
1515
my $script = $day-dir.add($day-dir.basename ~ '.raku');
1616
$day-dir.mkdir;
17-
for <input.example.txt input.actual.txt>.map({ $day-dir.add($_) }) {
18-
$_.spurt('') unless $_.f;
17+
unless $script.f {
18+
my %replace = :CLASS_NAME($class-name), :DAY_NUM($day-num);
19+
my $pat = /__(<{%replace.keys.join('|')}>)__/;
20+
my $template = $*PROGRAM-NAME.IO.sibling('template.raku').slurp;
21+
$script.spurt($template.subst($pat, { %replace<<$0>> }, :g));
22+
$script.chmod(0o755);
23+
}
24+
for <input.example.txt input.actual.txt>.map({ $day-dir.add($_) }) -> $input {
25+
$input.spurt('') unless $input.f;
26+
.spurt("part1: \npart2: \n") unless .f given $input.extension('expected');
1927
}
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);
6428
}

2021/runday

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+
# Run the AoC solution for a day on all of its input, saving logs to out/
9+
# Run an each change with ./watch day1 or watchexec ./runday day1
10+
11+
if [ $# -eq 0 ]; then
12+
echo "Usage: runday day1"
13+
exit 1
14+
fi
15+
BASEDIR="${0:h}"
16+
DAYDIR="$BASEDIR/$1"
17+
if [[ ! -d "$DAYDIR" ]]; then
18+
echo "No such directory: $DAYDIR"
19+
exit 1
20+
fi
21+
cd "$DAYDIR"
22+
PROGRAM="$1.raku"
23+
if [[ ! -f "$PROGRAM" ]]; then
24+
echo "Missing file: $PROGRAM"
25+
exit 1
26+
fi
27+
DATEFMT='%Y%m%d-%H%M%S'
28+
mkdir -p out
29+
OUTFILE="out/$(date +$DATEFMT).log"
30+
CMD=(raku "$PROGRAM" input.example*.txt input.actual.txt)
31+
echo
32+
echo "$CMD > $OUTFILE"
33+
echo
34+
$CMD 2>&1 | tee "$OUTFILE"

2021/template.raku

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
# https://adventofcode.com/2021/day/__DAY_NUM__
9+
10+
class __CLASS_NAME__ {
11+
has $.input is required;
12+
has $.input-lines = $!input.lines;
13+
14+
method solve-part1() of Str(Cool) {
15+
"TODO";
16+
}
17+
18+
method solve-part2() of Str(Cool) {
19+
"TODO";
20+
}
21+
}
22+
23+
class RunContext {
24+
has $.input-file;
25+
has $.input;
26+
has %.expected is rw;
27+
28+
method run-part(Int $part) {
29+
my $expected = $.expected«$part» // '';
30+
say "Running __CLASS_NAME__ part $part on $!input-file expecting '$expected'";
31+
my $solver = __CLASS_NAME__.new(:$!input);
32+
my $method = $solver.^find_method("solve-part$part").assuming: $solver;
33+
my $start = now;
34+
my $result = $method();
35+
my $end = now;
36+
say $result;
37+
"Part $part took %.3fms\n".printf(($end - $start) * 1000);
38+
if $expected {
39+
if $expected eq $result {
40+
say "\c[CHECK MARK] PASS with expected value '$result'";
41+
} else {
42+
say "\c[CROSS MARK] FAIL expected '$expected' but got '$result'";
43+
}
44+
}
45+
}
46+
}
47+
48+
sub MAIN(*@input-files) {
49+
for @input-files -> $input-file {
50+
if $input-file.IO.slurp -> $input {
51+
my $context = RunContext.new(:$input, :$input-file);
52+
if (my $expected-file = $input-file.IO.extension('expected')).f {
53+
for $expected-file.lines {
54+
$context.expected«$0» = $1.trim if m/part (\d+) \: \s* (.*)/;
55+
}
56+
}
57+
$context.run-part(1);
58+
say '';
59+
$context.run-part(2);
60+
} else {
61+
say "EMPTY INPUT FILE: $input-file";
62+
}
63+
say '=' x 40;
64+
}
65+
}

2021/watch

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,14 @@ if [ $# -eq 0 ]; then
1212
echo "Usage: watch day1"
1313
exit 1
1414
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-
15+
DIR="${0:h}/$1"
16+
if [[ ! -d "$DIR" ]]; then
17+
echo "No such directory: $DIR"
18+
exit 1
19+
fi
20+
cd "${0:h}/$1"
21+
CMD=("../runday" "$1")
22+
$CMD
3223
fswatch -0 -o *.raku input.* | while read -d "" event ; do
33-
runscript
24+
$CMD
3425
done

0 commit comments

Comments
 (0)