diff options
Diffstat (limited to 'challenge-165/polettix/perl')
| -rw-r--r-- | challenge-165/polettix/perl/ch-1.pl | 26 | ||||
| -rw-r--r-- | challenge-165/polettix/perl/ch-2.pl | 40 | ||||
| -rw-r--r-- | challenge-165/polettix/perl/cpanfile | 1 | ||||
| -rw-r--r-- | challenge-165/polettix/perl/cpanfile.snapshot | 9 |
4 files changed, 76 insertions, 0 deletions
diff --git a/challenge-165/polettix/perl/ch-1.pl b/challenge-165/polettix/perl/ch-1.pl new file mode 100644 index 0000000000..54f3a00864 --- /dev/null +++ b/challenge-165/polettix/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl +use v5.24; +use warnings; +use experimental 'signatures'; +no warnings 'experimental::signatures'; + +use Template::Perlish; + +my $input = shift // "53,10\n53,10,23,30\n23,30"; +say svg_for($input); + +sub svg_for ($text) { + state $tp2 = Template::Perlish->new->compile_as_sub( +q{<?xml version="1.0" encoding="UTF-8" standalone="yes"?> +<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> +<svg height="400" width="400" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg"> +[% for my $item (A 'lines') { + if ($item->@* == 2) { +%] <circle r="4" cx="[%= $item->[0] %]" cy="[%= $item->[1] %]" stroke-width="0" fill="#000000" /> +[% } else { +%] <polyline points="[%= join ' ', $item->@* %]" stroke="#ff0000" stroke-width="6" /> +[% } + } +%]</svg>}); + $tp2->({lines => [map {[ split m{,+}mxs ]} split m{\n+}mxs, $text]}); +} diff --git a/challenge-165/polettix/perl/ch-2.pl b/challenge-165/polettix/perl/ch-2.pl new file mode 100644 index 0000000000..daa5797c73 --- /dev/null +++ b/challenge-165/polettix/perl/ch-2.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl +use v5.24; +use warnings; +use experimental 'signatures'; +no warnings 'experimental::signatures'; + +my @points = map {[split m{\D+}]} grep { /\S/ } split m{\s+}mxs, ' + 333,129 39,189 140,156 292,134 393,52 160,166 362,122 13,193 + 341,104 320,113 109,177 203,152 343,100 225,110 23,186 282,102 + 284,98 205,133 297,114 292,126 339,112 327,79 253,136 61,169 + 128,176 346,72 316,103 124,162 65,181 159,137 212,116 337,86 + 215,136 153,137 390,104 100,180 76,188 77,181 69,195 92,186 + 275,96 250,147 34,174 213,134 186,129 189,154 361,82 363,89 + '; +my ($m, $q) = lse(@points); + +my $xmin = my $xmax = $points[0][0]; +for my $p (@points) { + my ($x, $y) = $p->@*; + if ($x < $xmin) { $xmin = $x } + elsif ($x > $xmax) { $xmax = $x } + say "$x,$y"; +} +my ($ymin, $ymax) = map { $m * $_ + $q } ($xmin, $xmax); +say "$xmin,$ymin,$xmax,$ymax"; + +sub lse (@points) { + my ($N, $X, $Y, $X2, $XY) = (scalar(@points), (0) x 4); + for my $p (@points) { + my ($x, $y) = $p->@*; + $X += $x; + $Y += $y; + $X2 += $x * $x; + $XY += $x * $y; + } + my $den = $N * $X2 - $X * $X; + my $m = ($N * $XY - $X * $Y) / $den; + my $q = ($X2 * $Y - $X * $XY) / $den; + return ($m, $q); +} diff --git a/challenge-165/polettix/perl/cpanfile b/challenge-165/polettix/perl/cpanfile new file mode 100644 index 0000000000..bd25c253cb --- /dev/null +++ b/challenge-165/polettix/perl/cpanfile @@ -0,0 +1 @@ +requires 'Template::Perlish'; diff --git a/challenge-165/polettix/perl/cpanfile.snapshot b/challenge-165/polettix/perl/cpanfile.snapshot new file mode 100644 index 0000000000..ced070ef92 --- /dev/null +++ b/challenge-165/polettix/perl/cpanfile.snapshot @@ -0,0 +1,9 @@ +# carton snapshot format: version 1.0 +DISTRIBUTIONS + Template-Perlish-1.56 + pathname: P/PO/POLETTIX/Template-Perlish-1.56.tar.gz + provides: + Template::Perlish 1.56 + requirements: + Module::Build::Tiny 0.034 + perl 5.008000 |
