-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
executable file
·140 lines (117 loc) · 3.13 KB
/
run
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
#!/usr/bin/env perl
use v5.24;
use warnings;
use experimental qw( signatures );
use Capture::Tiny qw( tee );
use File::chdir;
use FindBin qw( $Bin );
use Memoize;
use Path::Tiny;
use Term::ANSIColor qw( colored );
use YAML::XS qw( LoadFile );
use constant DATASET => $ENV{ADVENT_DATASET} || 'test';
sub path_to_year ( $year ) {
my $path = Path::Tiny::->new( $Bin )->absolute->child( $year );
$path->is_dir or die;
return $path;
}
sub path_to_day ( $year, $day ) {
my $path = path_to_year( $year )->child( $day );
$path->is_dir or die;
return $path;
}
sub path_to_solution ( $year, $day, $solution ) {
my $path = path_to_day( $year, $day )->child( $solution );
$path->is_dir or die;
return $path;
}
memoize 'get_config';
sub get_config ( $year, $day ) {
return LoadFile( path_to_day( $year, $day )->child( "day.yml" ) );
}
sub run_solution ( $year, $day, $solution ) {
my $env = DATASET;
my $config = get_config( $year, $day );
my $name = "$year/$day/$solution";
if ( $config->{solutions}{$solution}{slow}{$env} and not $ENV{ADVENT_SLOW} ) {
print colored( [ "magenta" ], "$name - skipped (slow)" ), "\n";
return;
}
print colored( [ "bold yellow" ], $name ), "\n";
my %env = %{ $config->{env}{$env} or die };
$env{ADVENT_INPUT} = path_to_day( $year, $day )->child( $env{ADVENT_INPUT} )->absolute;
$env{ADVENT_DATASET} = $env;
%ENV = ( %ENV, %env );
my ( $stdout, $stderr ) = tee {
local $CWD = path_to_solution( $year, $day, $solution )->stringify;
if ( -f 'Makefile' ) {
system( qw( make ) );
system( qw( make clean ) );
}
elsif ( -f 'Cargo.toml' ) {
system( qw( cargo run ) );
system( qw( cargo clean ) );
}
};
my $results = { 'NAME' => $name };
my %expect = %{ $config->{expect}{$env} or die };
for my $key ( sort keys %expect ) {
my $look_for = quotemeta( $key . ': ' . $expect{$key} );
$results->{$key} = !!( $stdout =~ /$look_for/ );
}
return $results;
}
sub run_day ( $year, $day ) {
my $config = eval { get_config( $year, $day ) }
or return;
map {
run_solution( $year, $day, $_ )
} sort keys %{ $config->{solutions} or die };
}
sub run_year ( $year ) {
my $y = path_to_year( $year );
my @days = sort map $_->basename, grep /(\d+)/, $y->children;
map { run_day( $year, $_ ) } @days;
}
sub run_all () {
(
run_year( '2022' ),
);
}
sub run_cli ( @argv ) {
my ( $YYYY, $DD, $SOLUTION ) = map { split q{/} } @argv;
my @results;
if ( $SOLUTION ) {
@results = run_solution( $YYYY, $DD, $SOLUTION );
}
elsif ( $DD ) {
@results = run_day( $YYYY, $DD );
}
elsif ( $YYYY ) {
@results = run_year( $YYYY );
}
else {
@results = run_all();
}
print "\n";
print colored( [ "bold cyan" ], "ADVENT_DATASET=" . DATASET ), "\n\n";
my $fails = 0;
for my $result ( @results ) {
my $name = delete $result->{NAME};
my @keys = sort keys %$result;
my $joined = join " ", map {
++$fails if !$result->{$_};
colored( $result->{$_} ? ["green"] : ["red"], $_ )
} @keys;
printf(
"%-32s %s\n",
colored( [ "bold white" ], "$name:" ),
$joined,
);
}
if ( $fails ) {
print colored( [ "bold red" ], "Failed $fails tests!" ), "\n";
}
return $fails;
}
exit( run_cli( @ARGV ) ) unless caller;