From 55810dacb6b1ea71ba84ac51918783711fefc6f5 Mon Sep 17 00:00:00 2001 From: Yitzchak Scott-Thoennes Date: Sun, 9 Nov 2025 16:55:43 -0500 Subject: use Run::WeeklyChallenge --- challenge-345/ysth/perl/ch-1.pl | 55 ++++--------------------------------- challenge-345/ysth/perl/ch-2.pl | 61 ++++++----------------------------------- 2 files changed, 15 insertions(+), 101 deletions(-) diff --git a/challenge-345/ysth/perl/ch-1.pl b/challenge-345/ysth/perl/ch-1.pl index d2f7dd49a1..92034fa16c 100644 --- a/challenge-345/ysth/perl/ch-1.pl +++ b/challenge-345/ysth/perl/ch-1.pl @@ -1,7 +1,4 @@ use 5.040; -use Cpanel::JSON::XS; -use JSON::Schema::Modern; -use List::Util (); sub longest_balanced_parentheses($string) { @@ -41,56 +38,16 @@ sub longest_balanced_parentheses($string) { } sub main() { - my $input_example = '"()()"'; - - my $input_schema = '{ - "type": "string" - }'; + require Run::WeeklyChallenge; my $run_solution = sub ($inputs) { longest_balanced_parentheses($inputs); }; - - my $json = Cpanel::JSON::XS->new->allow_nonref; - my $validator = JSON::Schema::Modern->new( 'specification_version' => 'draft2020-12', 'output_format' => 'flag' ); - my $schema = $json->decode($input_schema); - - my $errors; - - for my $inputs_json (@ARGV) { - say "Input: $inputs_json"; - - try { - my $inputs = $json->decode($inputs_json); - if (! $validator->evaluate($inputs, $schema)->valid) { - $errors = true; - say "Error: invalid input"; - next; - } - else { - try { - my $result = $run_solution->($inputs); - say "Output: $result"; - } - catch ($e) { - chomp $e; - say "Error: $e"; - } - } - } - catch ($e) { - $errors = true; - chomp $e; - say "Error: invalid json input: $e"; - next; - } - } - - if ($errors) { - say "Expected arguments like '$input_example'"; - } - - return; + my $inputs_example = '"()()"'; + my $inputs_schema_json = '{ + "type": "string" + }'; + Run::WeeklyChallenge::run_weekly_challenge($run_solution, $inputs_example, $inputs_schema_json); } main() unless caller; diff --git a/challenge-345/ysth/perl/ch-2.pl b/challenge-345/ysth/perl/ch-2.pl index 0f9d5965d4..996c02025b 100644 --- a/challenge-345/ysth/perl/ch-2.pl +++ b/challenge-345/ysth/perl/ch-2.pl @@ -1,7 +1,4 @@ use 5.040; -use Cpanel::JSON::XS; -use JSON::Schema::Modern; -use List::Util (); sub magic_expression($string, $target) { @@ -19,9 +16,15 @@ sub magic_expression($string, $target) { } sub main() { - my $input_example = '{"string":"123","target":6}'; + require Run::WeeklyChallenge; - my $input_schema = '{ + my $run_solution = sub ($inputs) { + # format results like: ("2*3+2", "2+3*2") + sprintf '(%s)', join ', ', map qq!"$_"!, + magic_expression($inputs->{'string'}, $inputs->{'target'}); + }; + my $inputs_example = '{"string":"123","target":6}'; + my $inputs_schema_json = '{ "type": "object", "properties": { "string": { @@ -33,53 +36,7 @@ sub main() { "required": ["string","target"], "additionalProperties": false }'; - - my $run_solution = sub ($inputs) { - # format results like: ("2*3+2", "2+3*2") - sprintf '(%s)', join ', ', map qq!"$_"!, - magic_expression($inputs->{'string'}, $inputs->{'target'}); - }; - - my $json = Cpanel::JSON::XS->new->allow_nonref; - my $validator = JSON::Schema::Modern->new( 'specification_version' => 'draft2020-12', 'output_format' => 'flag' ); - my $schema = $json->decode($input_schema); - - my $errors; - - for my $inputs_json (@ARGV) { - say "Input: $inputs_json"; - - try { - my $inputs = $json->decode($inputs_json); - if (! $validator->evaluate($inputs, $schema)->valid) { - $errors = true; - say "Error: invalid input"; - next; - } - else { - try { - my $result = $run_solution->($inputs); - say "Output: $result"; - } - catch ($e) { - chomp $e; - say "Error: $e"; - } - } - } - catch ($e) { - $errors = true; - chomp $e; - say "Error: invalid json input: $e"; - next; - } - } - - if ($errors) { - say "Expected arguments like '$input_example'"; - } - - return; + Run::WeeklyChallenge::run_weekly_challenge($run_solution, $inputs_example, $inputs_schema_json); } main() unless caller; -- cgit