-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_problem.pl
executable file
·256 lines (144 loc) · 3.78 KB
/
create_problem.pl
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env perl
die "This is not working right now!\n";
use Modern::Perl;
use HTML::TreeBuilder::XPath;
use WWW::Mechanize;
use Readonly;
Readonly::Scalar my $DATE_XPATH => q<//div[@id='main_page']//div[@class='rbcontent']/div[2]>;
Readonly::Scalar my $CONTENT_XPATH => q<//div[@id='main_page']//div[@class='problem_content']>;
Readonly::Scalar my $LINK => 'http://projecteuler.net/index.php?section=problems&id=%d';
Readonly::Scalar my $BASE_CODE => << '__END_CODE';
package Project::Euler::Problem::P%03d;
use Carp;
use Modern::Perl;
use Moose;
with 'Project::Euler::Problem::Base';
use Project::Euler::Lib::Types qw/ /; ### TEMPLATE ###
=head1 NAME
Project::Euler::Problem::P%03d - Solutions for problem %03d
=head1 VERSION
Version v0.1.0
=cut
use version 0.77; our $VERSION = qv("v0.1.0");
=head1 SYNOPSIS
L<< http://projecteuler.net/index.php?section=problems&id=%d >>
use Project::Euler::Problem::P%03d;
my $p%d = Project::Euler::Problem::P%03d->new;
my $default_answer = $p%d->solve;
=head1 DESCRIPTION
This module is used to solve problem #%03d
### TEMPLATE ###
=head1 Problem Attributes
### TEMPLATE ###
=cut
=head1 SETUP
=head2 Problem Number
%03d
=cut
sub _build_problem_number {
# Must be an int > 0
return %d;
}
=head2 Problem Name
### TEMPLATE ###
=cut
sub _build_problem_name {
return q{};### TEMPLATE ###
}
=head2 Problem Date
%s
=cut
sub _build_problem_date {
return q{%s};
}
=head2 Problem Desc
%s
=cut
sub _build_problem_desc {
return <<'__END_DESC';
%s
__END_DESC
}
=head2 Default Input
### TEMPLATE ###
=cut
sub _build_default_input {
return ;### TEMPLATE ###
}
=head2 Default Answer
%s
=cut
sub _build_default_answer {
%s
}
=head2 Has Input?
### TEMPLATE ###
=cut
### TEMPLATE ###
#has '+has_input' => (default => 0);
=head2 Help Message
### TEMPLATE ###
=cut
sub _build_help_message {
return <<'__END_HELP';
### TEMPLATE ###
__END_HELP
}
=head1 INTERNAL FUNCTIONS
=head2 Validate Input
### TEMPLATE ###
=cut
sub _check_input {
### TEMPLATE ###
}
=head2 Solving the problem
### TEMPLATE ###
=cut
sub _solve_problem {
my ($self, $input) = @_;
$input //= $self->default_input;
### TEMPLATE ###
}
=head1 AUTHOR
Adam Lesperance, C<< <lespea at cpan.org> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-project-euler at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Project-Euler>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Project::Euler::Problem::P%03d
=head1 ACKNOWLEDGEMENTS
=head1 COPYRIGHT & LICENSE
Copyright 2009 Adam Lesperance.
This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
=cut
# Cleanup the Moose stuff
no Moose;
__PACKAGE__->meta->make_immutable;
1; # End of Project::Euler::Problem::P%03d
__END_CODE
my ($num, $answer) = @ARGV;
$answer //= '### TEMPLATE ###';
die unless defined $num;
my $url = sprintf($LINK, $num);
my $mech = WWW::Mechanize->new;
my $resp = $mech->get($url);
my $tree = HTML::TreeBuilder::XPath->new_from_content($resp->as_string);
$tree->elementify;
my $date = $tree->findvalue($DATE_XPATH);
my $desc = $tree->findvalue($CONTENT_XPATH);
$tree->delete;
die "no date\n" unless defined $date;
die "no desc\n" unless defined $desc;
printf( $BASE_CODE,
($num) x 11,
($date) x 2,
($desc) x 2,
$answer,
"return $answer;",
($num) x 2
);