diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-04-24 05:52:39 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-24 05:52:39 +0100 |
| commit | b879b41d3396953b86c5fd5079b3c87f15a72a92 (patch) | |
| tree | 427691e9599000fdb9ee0a53885f86174d1743f3 | |
| parent | 5087ad4188040c5629c5aa28b662d3fe842b6b29 (diff) | |
| parent | 692966b41c713390123cff3e49433bfd86f8ecd3 (diff) | |
| download | perlweeklychallenge-club-b879b41d3396953b86c5fd5079b3c87f15a72a92.tar.gz perlweeklychallenge-club-b879b41d3396953b86c5fd5079b3c87f15a72a92.tar.bz2 perlweeklychallenge-club-b879b41d3396953b86c5fd5079b3c87f15a72a92.zip | |
Merge pull request #7964 from jaldhar/challenge-213
Challenge 213 by Jaldhar H. Vyas.
| -rw-r--r-- | challenge-213/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-213/jaldhar-h-vyas/perl/ch-1.pl | 20 | ||||
| -rwxr-xr-x | challenge-213/jaldhar-h-vyas/perl/ch-2.pl | 121 | ||||
| -rwxr-xr-x | challenge-213/jaldhar-h-vyas/raku/ch-1.raku | 13 | ||||
| -rwxr-xr-x | challenge-213/jaldhar-h-vyas/raku/ch-2.raku | 116 |
5 files changed, 271 insertions, 0 deletions
diff --git a/challenge-213/jaldhar-h-vyas/blog.txt b/challenge-213/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..88a95c0589 --- /dev/null +++ b/challenge-213/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2023/04/perl_weekly_challenge_week_213.html
\ No newline at end of file diff --git a/challenge-213/jaldhar-h-vyas/perl/ch-1.pl b/challenge-213/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..80e654e62a --- /dev/null +++ b/challenge-213/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl +use 5.030; +use warnings; + +my @list = @ARGV; +my @odd; +my @even; + +for my $i (@list) { + if ($i % 2 == 0) { + push @even, $i; + } else { + push @odd, $i; + } +} + +my @results = sort { $a <=> $b} @even; +push @results, sort { $a <=> $b } @odd; + +say q{(}, (join q{,}, @results), q{)}; diff --git a/challenge-213/jaldhar-h-vyas/perl/ch-2.pl b/challenge-213/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..08855856fb --- /dev/null +++ b/challenge-213/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,121 @@ +#!/usr/bin/perl +use 5.030; +use warnings; + +sub findKeyFor { + my ($routes, $target) = @_; + + for my $i (0 .. scalar @{$routes} - 1) { + for my $j (0 .. scalar @{$routes->[$i]} - 1) { + if ($routes->[$i]->[$j] == $target) { + return "$i-$j"; + } + } + } + return ""; +} + +sub makeGraph { + my ($routes) = @_; + my %graph; + + for my $i (0 .. scalar @{$routes} - 1) { + for my $j (0 .. scalar @{$routes->[$i]} - 1) { + if ($j != 0) { + push @{$graph{"$i-$j"}}, "$i-" . ($j - 1); + } + my $end = scalar @{$routes->[$i]} - 1; + if ($j != $end) { + push @{$graph{"$i-$j"}}, "$i-" . ($j + 1); + } else { + my $l = makeLink($routes, $i, @{$routes->[$i]}[$end]); + unless ($l eq q{}) { + push @{$graph{"$i-$end"}}, $l; + push @{$graph{$l}}, "$i-$end"; + } + } + } + } + + return \%graph; +} + +sub makeLink { + my ($routes, $currentRoute, $value) = @_; + for my $i (0 .. scalar @{$routes} - 1) { + if ($i == $currentRoute) { + next; + } + for my $j (0 .. scalar @{$routes->[$i]} - 1) { + if ($routes->[$i][$j] == $value) { + return "$i-$j"; + } + } + } + return ""; +} + +sub traverse { + my ($graph, $startNode, $endNode) = @_; + my %visited; + my @queue = ( [$startNode] ); + + while (scalar @queue) { + my $path = shift @queue; + my $node = @{$path}[-1]; + if ($node eq $endNode) { + return @{$path}; + } + + for my $v (@{$graph->{$node}}) { + if (!exists $visited{$v}) { + $visited{$v} = undef; + my @next = @{$path}; + push @next, $v; + push @queue, \@next; + } + } + } +} + + +sub findShortestPath { + my ($routes, $source, $destination) = @_; + my $graph = makeGraph($routes); + my $startNode = findKeyFor($routes, $source); + my $endNode = findKeyFor($routes, $destination); + + if ($startNode eq q{} || $endNode eq q{}) { + return (); + } + + return traverse($graph, $startNode, $endNode); +} + +my $source = shift; +my $destination = shift; +my @routes; +for my $route (@ARGV) { + push @routes, [ split /\s+/, $route ]; +} + +my @path = findShortestPath(\@routes, $source, $destination); +unless (scalar @path) { + say -1; + exit; +} + +my ($route, $elem) = split q{-}, shift @path; +my $currentRoute = $route; +my @results = ( $routes[$route][$elem] ); + +for my $node (@path) { + my ($route, $elem) = split q{-}, $node; + if ($route == $currentRoute) { + push @results, $routes[$route][$elem]; + } else { + $currentRoute = $route; + } +} + +say q{(}, (join q{,}, @results), q{)}; diff --git a/challenge-213/jaldhar-h-vyas/raku/ch-1.raku b/challenge-213/jaldhar-h-vyas/raku/ch-1.raku new file mode 100755 index 0000000000..9838b5242c --- /dev/null +++ b/challenge-213/jaldhar-h-vyas/raku/ch-1.raku @@ -0,0 +1,13 @@ +#!/usr/bin/raku + +sub MAIN( + *@list +) { + + @list.classify( { $_ %% 2 ?? 'even' !! 'odd' }, :into( my %class; ) ); + + my @results = %class{'even'}.sort({ $^a <=> $^b }); + @results.push(| %class{'odd'}.sort({ $^a <=> $^b }) ); + + say q{(}, @results.join(q{,}), q{)}; +}
\ No newline at end of file diff --git a/challenge-213/jaldhar-h-vyas/raku/ch-2.raku b/challenge-213/jaldhar-h-vyas/raku/ch-2.raku new file mode 100755 index 0000000000..7ce1fb4483 --- /dev/null +++ b/challenge-213/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,116 @@ +#!/usr/bin/raku + +sub findKeyFor(@routes, $target) { + for 0 .. @routes.end -> $i { + for 0 .. @routes[$i].end -> $j { + if @routes[$i;$j] == $target { + return "$i-$j"; + } + } + } + return ""; +} + +sub makeGraph(@routes) { + my %graph; + + for 0 ..^ @routes.elems -> $i { + for 0 ..^ @routes[$i].elems -> $j { + if $j != 0 { + %graph{"$i-$j"}.push("$i-" ~ $j - 1); + } + if $j != @routes[$i].end { + %graph{"$i-$j"}.push("$i-" ~ $j + 1); + } else { + my $l = makeLink(@routes, $i, @routes[$i][@routes[$i].end]); + unless $l eq q{} { + %graph{"$i-" ~ @routes[$i].end}.push($l); + %graph{$l}.push("$i-" ~ @routes[$i].end); + } + } + } + } + return %graph; +} + +sub makeLink(@routes, $currentRoute, $value) { + for 0 .. @routes.end -> $i { + if $i == $currentRoute { + next; + } + for 0 ..^ @routes[$i].elems -> $j { + if @routes[$i;$j] == $value { + return "$i-$j"; + } + } + } + return ""; +} + +sub traverse(%graph, $startNode) { + my %visited; + my @queue = ( $startNode ); + %visited{$startNode} = True; + + while @queue.elems { + my @path = @queue.shift.flat; + my $node = @path[*-1]; + take $node, @path; + + for %graph{$node}.values -> $v { + if !%visited{$v} { + %visited{$v} = True; + @queue.push((my @next = @path).push($v)); + } + } + } + + take Nil, (); +} + +sub findShortestPath(@routes, $source, $destination) { + my %graph = makeGraph(@routes); + my $startNode = findKeyFor(@routes, $source); + my $endNode = findKeyFor(@routes, $destination); + + if $startNode eq q{} || $endNode eq q{} { + return (); + } + + for gather traverse(%graph, $startNode) -> ($node, @path) { + if $node ~~ Nil { + return (); + } + + if $node ~~ $endNode { + return @path; + } + } +} + +sub MAIN( + $source, $destination, *@list +) { + my @routes = @list.map({ $_.split(/\s+/) }); + + my @path = findShortestPath(@routes, $source, $destination); + unless @path.elems { + say -1; + exit; + } + + my ($route, $elem) = @path.shift.split(q{-}); + my $currentRoute = $route; + my @results = ( @routes[$route;$elem] ); + + for @path -> $node { + my ($route, $elem) = $node.split(q{-}); + if $route == $currentRoute { + @results.push(@routes[$route;$elem]); + } else { + $currentRoute = $route; + } + } + + say q{(}, @results.join(q{,}), q{)}; +}
\ No newline at end of file |
