-
Notifications
You must be signed in to change notification settings - Fork 0
/
start
executable file
·90 lines (70 loc) · 1.75 KB
/
start
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
#!/usr/bin/env perl
use v5.38;
use autodie;
use HTTP::Cookies;
use LWP::UserAgent;
use File::Path qw(make_path);
use File::Basename qw(dirname);
sub slurp ($file_name)
{
local $/;
open my $fh, '<', $file_name;
my $content = readline $fh;
chomp $content;
return $content;
}
my $year = shift || die 'requires year';
my $day = shift;
my @days;
if ($day && $day =~ m{ \A (\d+) (?: \.\. (\d+))? \z }x) {
my $day_from = $1;
my $day_to = $2 || $day_from;
@days = $day_from .. $day_to;
}
die 'requires day number(s)'
if !@days;
foreach my $day (@days) {
my $solution_content = <<~PERL;
package Day${day}::Solution;
use class;
with 'Solution';
sub part_1 (\$self)
{
...
}
sub part_2 (\$self)
{
...
}
PERL
my $fetch_input = sub {
my $jar = HTTP::Cookies->new;
$jar->set_cookie(0, session => slurp('.session'), '/', 'adventofcode.com', 443, 0, 0, 9999, 0);
say 'Fetching input data...';
my $ua = LWP::UserAgent->new(cookie_jar => $jar, timeout => 10);
my $response = $ua->get("https://adventofcode.com/${year}/day/${day}/input");
if (!$response->is_success) {
die $response->status_line;
}
return $response->decoded_content;
};
my %new_files = (
"${year}/input/day${day}.txt" => $fetch_input,
"${year}/test/day${day}.txt" => '',
"${year}/test/expected/day${day}_part1.txt" => '',
"${year}/test/expected/day${day}_part2.txt" => '',
"${year}/lib/Day${day}/Solution.pm" => $solution_content,
);
foreach my $file_name (keys %new_files) {
my $content = $new_files{$file_name};
make_path dirname $file_name;
next if -e $file_name;
$content = $content->()
if ref $content eq 'CODE';
say "Creating $file_name...";
open my $fh, '>', $file_name;
print {$fh} $content;
}
say "Done with day $day";
}
say 'All done!';