aboutsummaryrefslogtreecommitdiff
path: root/challenge-165
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-05-18 07:43:39 +0100
committerGitHub <noreply@github.com>2022-05-18 07:43:39 +0100
commitae81754ae7ae844de9734461e8dc18daaee44767 (patch)
tree4b780d4172d556713ae5e725a191ad9258a47519 /challenge-165
parent0aff17755ac6bd66d16362d411dba1dc8cd6b92d (diff)
parentd483056efba4eff2a2f9296c3322ee86fbb62808 (diff)
downloadperlweeklychallenge-club-ae81754ae7ae844de9734461e8dc18daaee44767.tar.gz
perlweeklychallenge-club-ae81754ae7ae844de9734461e8dc18daaee44767.tar.bz2
perlweeklychallenge-club-ae81754ae7ae844de9734461e8dc18daaee44767.zip
Merge pull request #6122 from polettix/polettix/pwc165
Add polettix's solution to challenge-165
Diffstat (limited to 'challenge-165')
-rw-r--r--challenge-165/polettix/blog.txt1
-rw-r--r--challenge-165/polettix/blog1.txt1
-rw-r--r--challenge-165/polettix/perl/ch-1.pl26
-rw-r--r--challenge-165/polettix/perl/ch-2.pl40
-rw-r--r--challenge-165/polettix/perl/cpanfile1
-rw-r--r--challenge-165/polettix/perl/cpanfile.snapshot9
-rw-r--r--challenge-165/polettix/raku/ch-1.raku49
-rw-r--r--challenge-165/polettix/raku/ch-2.raku35
8 files changed, 162 insertions, 0 deletions
diff --git a/challenge-165/polettix/blog.txt b/challenge-165/polettix/blog.txt
new file mode 100644
index 0000000000..ef84ffe033
--- /dev/null
+++ b/challenge-165/polettix/blog.txt
@@ -0,0 +1 @@
+https://github.polettix.it/ETOOBUSY/2022/05/17/pwc165-scalable-vector-graphics/
diff --git a/challenge-165/polettix/blog1.txt b/challenge-165/polettix/blog1.txt
new file mode 100644
index 0000000000..70be25adb2
--- /dev/null
+++ b/challenge-165/polettix/blog1.txt
@@ -0,0 +1 @@
+https://github.polettix.it/ETOOBUSY/2022/05/18/pwc165-line-of-best-fit/
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
diff --git a/challenge-165/polettix/raku/ch-1.raku b/challenge-165/polettix/raku/ch-1.raku
new file mode 100644
index 0000000000..0da4c8831b
--- /dev/null
+++ b/challenge-165/polettix/raku/ch-1.raku
@@ -0,0 +1,49 @@
+#!/usr/bin/env raku
+use v6;
+sub MAIN (Str $input = "53,10\n53,10,23,30\n23,30") {
+ put svg-for($input)
+}
+
+sub svg-for ($input) {
+ (
+ gather {
+ take '<?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">';
+ take open-tag('svg', width => 400, height => 400, xmlns => 'http://www.w3.org/2000/svg');
+ for $input.lines -> $line {
+ my @nums = $line.split(/ \, /);
+ take @nums == 2 ?? point(@nums) !! line(@nums);
+ };
+ take close-tag('svg');
+ }
+ ).join("\n");
+}
+
+sub open-tag ($tag, *%args) {
+ ("<$tag", %args.kv.map(-> $k, $v {qq<$k="$v">}), '>').join(' ');
+}
+
+sub oneshot-tag ($tag, *%args) {
+ ("<$tag", %args.kv.map(-> $k, $v {qq<$k="$v">}), '/>').join(' ');
+}
+
+sub close-tag ($tag) { return "</$tag>" }
+
+sub point (@p, *%args) {
+ my %pargs =
+ 'cx', @p[0],
+ 'cy', @p[1],
+ 'r', 4,
+ 'stroke-width', 0,
+ 'fill', '#000000'
+ ;
+ oneshot-tag('circle', |%pargs, |%args);
+}
+
+sub line (@ps, *%args) {
+ my %pargs =
+ 'points', @ps.join(' '),
+ 'stroke-width', 6,
+ 'stroke', '#ff0000';
+ oneshot-tag('polyline', |%pargs, |%args);
+}
diff --git a/challenge-165/polettix/raku/ch-2.raku b/challenge-165/polettix/raku/ch-2.raku
new file mode 100644
index 0000000000..3bc525b484
--- /dev/null
+++ b/challenge-165/polettix/raku/ch-2.raku
@@ -0,0 +1,35 @@
+#!/usr/bin/env raku
+use v6;
+sub MAIN {
+ my @points = '
+ 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
+ '.comb(/\S+/).map({.split(/\,/).Array});
+ my ($m, $q) = lse(@points);
+ my $xmin = my $xmax = @points[0][0];
+ for @points -> ($x, $y) {
+ if $x < $xmin { $xmin = $x } elsif $x > $xmax { $xmax = $x }
+ put "$x,$y";
+ }
+ my ($ymin, $ymax) = ($xmin, $xmax).map: {$m * $_ + $q};
+ put "$xmin,$ymin,$xmax,$ymax";
+}
+
+sub lse (@points) {
+ my $N = @points.elems;
+ my ($X, $Y, $X2, $XY) = 0 xx 4;
+ for @points -> ($x, $y) {
+ $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;
+}