diff options
| -rw-r--r-- | challenge-025/ruben-westerberg/README | 17 | ||||
| -rwxr-xr-x | challenge-025/ruben-westerberg/perl5/ch-1.pl | 48 | ||||
| -rwxr-xr-x | challenge-025/ruben-westerberg/perl5/ch-2.pl | 40 | ||||
| -rwxr-xr-x | challenge-025/ruben-westerberg/perl6/ch-1.p6 | 39 | ||||
| -rwxr-xr-x | challenge-025/ruben-westerberg/perl6/ch-2.p6 | 26 |
5 files changed, 161 insertions, 9 deletions
diff --git a/challenge-025/ruben-westerberg/README b/challenge-025/ruben-westerberg/README index bd6dab73ef..e7a3e41132 100644 --- a/challenge-025/ruben-westerberg/README +++ b/challenge-025/ruben-westerberg/README @@ -2,16 +2,15 @@ Solution by Ruben Westerberg ch-1.pl and ch-1.p6 === -Shortest script that doesn't throw an error... - usage - perl ch-1.pl - perl6 ch-1.pl - Scripts are 0 bytes in size +Longest Pokemon name sequence. +Simply run the program to see the sequence update. Last one printed in the longest one + ch-2.pl and ch-2.p6 === -Builds and prints out a inverted index of all words in in put documents. Text files are provided as command line arguments. -The output of the program show the word, the locations of this word in each of the files. -usage example: - ./ch-2.pl ../text/{beowulf.txt,pride.txt} +Implements the chaochiper +usage example (encode and decode pipeline reading from standard input) + ch-2.pl | ch-2.pl decode + + ch-2.p6 | ch-2.p6 --decode diff --git a/challenge-025/ruben-westerberg/perl5/ch-1.pl b/challenge-025/ruben-westerberg/perl5/ch-1.pl new file mode 100755 index 0000000000..60e2e9bf57 --- /dev/null +++ b/challenge-025/ruben-westerberg/perl5/ch-1.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl +# +use strict; +use warnings; +use List::Util qw<all none any>; + +my @pokemon= do { open my $fh, "<", "../pokemon.txt"; <$fh>}; +chop @pokemon; +#@pokemon=@pokemon[0..49]; + +my @stack; +my ($max, $node)=(0, undef); + +my $root={name=>"", parent=>undef, depth=>0}; +push @stack, $root; + +while (@stack){ + my @result= process(shift @stack); + push @stack, @result; +} + +print join ",", ppath($node); +sub process { + my $tree=shift; + my @path=ppath($tree); + my @next=grep { !defined($tree->{parent}) || (substr($_, 0,1) eq substr($tree->{name},-1,1 ))} @pokemon; + @next=sort grep { my $n=$_; none {$n eq $_ } @path } @next; + if (@next && $tree->{depth} > $max) { + #print "We have max \n"; + $max= $tree->{depth}; + $node=$tree; + print "@path\n"; + } + + map {{name=>$_, parent=>$tree, depth=>$tree->{depth}+1} } @next; +} + + +sub ppath { + my $node=shift; + my @res; + while (defined $node->{parent}) { + my $n=$node; + $node=$node->{parent}; + unshift @res, $n->{name}; + } + @res; +} diff --git a/challenge-025/ruben-westerberg/perl5/ch-2.pl b/challenge-025/ruben-westerberg/perl5/ch-2.pl new file mode 100755 index 0000000000..2410e796c3 --- /dev/null +++ b/challenge-025/ruben-westerberg/perl5/ch-2.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl + +use warnings; +use strict; + +my $decode=$ARGV[0]//undef; +my @alpha= ("a".."z","A".."Z"," ", qw(? ! . : >)); +my @alpha1=@alpha; +my @alpha2=@alpha; +$|=1; +while (<STDIN>) { + chomp; + for (split "", $_) { + print chaochiper(\@alpha1,\@alpha2, $_, $decode); + } + print "\n"; +} +sub chaochiper { + + my ($a1,$a2,$c,$decode)=@_; + my ($p,$ct); + if ($decode) { + $p= (grep { $$a2[$_] eq $c } 0..@$a2-1)[0]; + $ct=$$a1[$p]; + } + else { + $p= (grep { $$a1[$_] eq $c } 0..@$a1-1)[0]; + $ct=$$a2[$p]; + } + + push @$a1, splice @$a1, 0, $p+1; + push @$a2, splice @$a2, 0, $p; + + { + use integer; + splice(@$a1, @$a1 / 2,0, splice(@$a1,2,1)); + splice(@$a2, @$a2 / 2,0 ,splice(@$a2,1,1)); + } + $ct; +} diff --git a/challenge-025/ruben-westerberg/perl6/ch-1.p6 b/challenge-025/ruben-westerberg/perl6/ch-1.p6 new file mode 100755 index 0000000000..c95233a645 --- /dev/null +++ b/challenge-025/ruben-westerberg/perl6/ch-1.p6 @@ -0,0 +1,39 @@ +#!/usr/bin/env perl6 +# +my @pokemon="../pokemon.txt".IO.lines; +#@pokemon=@pokemon[^50]; +my %root=(name=>Nil, parent=>Nil, depth=>0); +my $max=0; +my $node=Nil; +my @stack.push: %root; +my $stopFlag=False; +my $channel=Channel.new; +my $counter=0; +signal(SIGINT).tap({$stopFlag=True}); + +while @stack and !$stopFlag { + @stack.append: process @stack.shift; +} + +say pokepath($node); + +sub process(%tree) { + my $next:=@pokemon.grep: {!%tree<parent>.defined||.starts-with: %tree<name>.substr: *-1,1 }; + my @path=pokepath(%tree); + $next:= sort keys $next (-) @path; + if %tree<depth> > $max { + $max=%tree<depth>; + $node=%tree; + say @path; + } + + $next.map({ + %( name=>$_, parent=>%tree, depth=>%tree<depth>+1 ); + }) ; +} +sub pokepath($node is copy) { + reverse gather while $node<parent>.defined { + take $node<name>; + $node=$node.<parent>; + } +} diff --git a/challenge-025/ruben-westerberg/perl6/ch-2.p6 b/challenge-025/ruben-westerberg/perl6/ch-2.p6 new file mode 100755 index 0000000000..aed270d8da --- /dev/null +++ b/challenge-025/ruben-westerberg/perl6/ch-2.p6 @@ -0,0 +1,26 @@ +#!/usr/bin/env perl6 + +sub MAIN (Bool :$decode=False) { + $*OUT.out-buffer=0; + my @alpha=("a".."z","A".."Z"," ", <? ! . : >)[*;*]; + my @alpha1=@alpha; + my @alpha2=@alpha; + for $*IN.lines { + put join "", .comb.map: { chaochiper(@alpha1,@alpha2, $_, :$decode) }; + }; +} + +sub chaochiper(@alpha1,@alpha2, $c, :$decode=False){ + my $p=($decode??@alpha2!!@alpha1).grep($c,:k)[0]; + my $ct=($decode??@alpha1!!@alpha2)[$p]; + + given @alpha1 { + .=rotate($p+1); + .splice(.elems div 2, 0, .splice(2,1)); + } + given @alpha2 { + .=rotate($p); + .splice(.elems div 2, 0, .splice(1,1)); + } + $ct; +} |
