-
Notifications
You must be signed in to change notification settings - Fork 1
/
template.raku
executable file
·104 lines (91 loc) · 2.5 KB
/
template.raku
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
#!/usr/bin/env raku
# Copyright 2021 Google LLC
#
# 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.
#
# https://adventofcode.com/2021/day/__DAY_NUM__
use v6.d;
use fatal;
grammar InputFormat {
rule TOP { <line>+ }
token ws { <!ww>\h* }
token num { \d+ }
rule line { ^^ .*? $$ \n }
}
class BaseActions {
method TOP($/) { make $<line>».made }
method num($/) { make $/.Int }
method line($/) { make $/.chomp }
}
class Solver {
has Str $.input is required;
has $.parsed = InputFormat.parse($!input, :actions(self.new-actions)) || die 'Parse failed';
has @.lines = $!parsed<line>».made;
submethod new-actions( --> BaseActions) { !!! }
}
class Part1 is Solver {
class Actions is BaseActions {
}
submethod new-actions() { Actions.new }
method solve( --> Str(Cool)) {
"TODO";
}
}
class Part2 is Solver {
class Actions is BaseActions {
}
submethod new-actions() { Actions.new }
method solve( --> Str(Cool)) {
"TODO";
}
}
class RunContext {
has $.input-file;
has $.input;
has %.expected is rw;
has @.passed;
method run-part(Solver:U $part) {
my $num = $part.^name.comb(/\d+/).head;
my $expected = $.expected«$num» // '';
$*ERR.say: "Running __CLASS_NAME__ part $num on $!input-file expecting '$expected'";
my $start = now;
my $solver = $part.new(:$!input);
my $result = $solver.solve();
my $end = now;
put "part$num: $result";
"Part $num took %.3fms\n".printf(($end - $start) * 1000);
@!passed.push($result eq 'TODO' || $expected && $expected eq $result);
if $expected {
if $expected eq $result {
$*ERR.say: "\c[CHECK MARK] PASS with expected value '$result'";
} else {
$*ERR.say: "\c[CROSS MARK] FAIL expected '$expected' but got '$result'";
}
}
$*OUT.flush;
$*ERR.flush;
}
}
sub MAIN(*@input-files) {
my $exit = all();
for @input-files -> $input-file {
if $input-file.IO.slurp -> $input {
my $context = RunContext.new(:$input, :$input-file);
if (my $expected-file = $input-file.IO.extension('expected')).f {
for $expected-file.lines {
$context.expected«$0» = $1.trim if m/part (\d+) \: \s* (.*)/;
}
}
$context.run-part(Part1);
$*ERR.say: '';
$context.run-part(Part2);
$exit &= all($context.passed);
} else {
$*ERR.say: "EMPTY INPUT FILE: $input-file";
}
$*ERR.say: '=' x 40;
}
exit $exit ?? 0 !! 1;
}