diff options
Diffstat (limited to 'challenge-046')
76 files changed, 2403 insertions, 36 deletions
diff --git a/challenge-046/alicia-bielsa/perl/ch-1.pl b/challenge-046/alicia-bielsa/perl/ch-1.pl new file mode 100644 index 0000000000..e2ff41d1b4 --- /dev/null +++ b/challenge-046/alicia-bielsa/perl/ch-1.pl @@ -0,0 +1,55 @@ + +use strict; +use warnings; +use Data::Dumper; + +#Cryptic Message +#The communication system of an office is broken and message received are not completely reliable. To send message Hello, it ended up sending these following: +# +#H x l 4 ! +#c e - l o +#z e 6 l g +#H W l v R +#q 9 m # o +#Similary another day we received a message repeatedly like below: +# +#P + 2 l ! a t o +#1 e 8 0 R $ 4 u +#5 - r ] + a > / +#P x w l b 3 k \ +#2 e 3 5 R 8 y u +#< ! r ^ ( ) k 0 +#Write a script to decrypt the above repeated message (one message repeated 6 times). +# +#HINT: Look for characters repeated in a particular position in all six messages received. + +my @aMessage1 = qw(P + 2 l ! a t o); +my @aMessage2 = qw(1 e 8 0 R $ 4 u); +my @aMessage3 = qw(5 - r ] + a > /); +my @aMessage4 = qw(P x w l b 3 k \\); +my @aMessage5 = qw(2 e 3 5 R 8 y u); +my @aMessage6 = qw(< ! r ^ ( ) k 0); +my @aCharactersRepeated = (); +my $lengthMessage = scalar(@aMessage1); +my @aAllMessages = (@aMessage1, @aMessage2 ,@aMessage3, @aMessage4, @aMessage5 ,@aMessage6); +my %hCharacters = (); + +foreach my $indexAllMesages (0..$#aAllMessages){ + my $indexMessage = $indexAllMesages % $lengthMessage; + my $currentCharacter = $aAllMessages[$indexAllMesages]; + unless (exists($hCharacters{$indexMessage})){ + $hCharacters{$indexMessage} =(); + } + unless (exists($hCharacters{$indexMessage}{$currentCharacter})){ + $hCharacters{$indexMessage}{$currentCharacter} = 0 ; + } + $hCharacters{$indexMessage}{$currentCharacter} ++; + if ($hCharacters{$indexMessage}{$currentCharacter} == 2 ){ + $aCharactersRepeated[$indexMessage] = $currentCharacter; + } +} +print "Decrypted Message:"; +foreach my $character (@aCharactersRepeated){ + print "$character"; +} +print "\n"; diff --git a/challenge-046/alicia-bielsa/perl/ch-2.pl b/challenge-046/alicia-bielsa/perl/ch-2.pl new file mode 100644 index 0000000000..26e3cefb6b --- /dev/null +++ b/challenge-046/alicia-bielsa/perl/ch-2.pl @@ -0,0 +1,32 @@ +#Is the room open? +#There are 500 rooms in a hotel with 500 employees having keys to all the rooms. +#The first employee opened main entrance door of all the rooms. The second employee then closed the doors of room numbers 2,4,6,8,10 and so on to 500. The third employee then closed the door if it was opened or opened the door if it was closed of rooms 3,6,9,12,15 and so on to 500. Similarly the fourth employee did the same as the third but only room numbers 4,8,12,16 and so on to 500. This goes on until all employees has had a turn. +# +#Write a script to find out all the rooms still open at the end. + +use strict; +use warnings; + +my $TOTAL = 500; +my @aRooms = (0) x $TOTAL; +foreach my $employee (1..$TOTAL){ + foreach my $door ($employee..$TOTAL){ + if ($door % $employee == 0 ){ + $aRooms[$door-1] = flipStatus ($aRooms[$door-1]); + } + } +} + +foreach my $room (0..$#aRooms){ + if ($aRooms[$room]){ + $room ++; + print "Room $room opened\n"; + } +} +sub flipStatus { + my $status = shift; + if ($status){ + return 0; + } + return 1; +}
\ No newline at end of file diff --git a/challenge-046/andrezgz/perl/ch-1.pl b/challenge-046/andrezgz/perl/ch-1.pl new file mode 100644 index 0000000000..e2a16bcf71 --- /dev/null +++ b/challenge-046/andrezgz/perl/ch-1.pl @@ -0,0 +1,57 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-046/ +# Task #1 +# Cryptic Message +# The communication system of an office is broken and message received +# are not completely reliable. To send message Hello, +# it ended up sending these following: +# +# H x l 4 ! +# c e - l o +# z e 6 l g +# H W l v R +# q 9 m # o +# Similary another day we received a message repeatedly like below: +# +# P + 2 l ! a t o +# 1 e 8 0 R $ 4 u +# 5 - r ] + a > / +# P x w l b 3 k \ +# 2 e 3 5 R 8 y u +# < ! r ^ ( ) k 0 +# Write a script to decrypt the above repeated message (one message repeated 6 times). +# +# HINT: Look for characters repeated in a particular position in all six messages received. + +use strict; +use warnings; + +my $msg = [ + [qw# P + 2 l ! a t o #], + [qw# 1 e 8 0 R $ 4 u #], + [qw# 5 - r ] + a > / #], + [qw# P x w l b 3 k \ #], + [qw# 2 e 3 5 R 8 y u #], + [qw# < ! r ^ ( ) k 0 #], +]; + +my $msg_len = scalar @{$msg->[0]}; +my @decrypted = ('?') x $msg_len; + +for my $i (1 .. @{$msg}-1) { + for my $j (0 .. $i-1) { + for (0 .. $msg_len-1) { + $decrypted[$_] = $msg->[$i]->[$_] if ($msg->[$i]->[$_] eq $msg->[$j]->[$_]); + } + } + print @decrypted,"\n"; +} + +__END__ +./ch-1.pl +???????? +?????a?? +P??l?a?? +Pe?lRa?u +PerlRaku diff --git a/challenge-046/andrezgz/perl/ch-2.pl b/challenge-046/andrezgz/perl/ch-2.pl new file mode 100644 index 0000000000..7a0e0c5565 --- /dev/null +++ b/challenge-046/andrezgz/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-046/ +# Task #2 +# Is the room open? +# There are 500 rooms in a hotel with 500 employees having keys to all the rooms. +# The first employee opened main entrance door of all the rooms. +# The second employee then closed the doors of room numbers 2,4,6,8,10 and so on to 500. +# The third employee then closed the door if it was opened or opened the door +# if it was closed of rooms 3,6,9,12,15 and so on to 500. +# Similarly the fourth employee did the same as the third +# but only room numbers 4,8,12,16 and so on to 500. +# This goes on until all employees has had a turn. +# +# Write a script to find out all the rooms still open at the end. + +use strict; +use warnings; + +use constant MAX => 500; + +my @rooms = (-1) x MAX; # all rooms are closed +unshift @rooms, 0; # add room "0" for simplicity + +foreach my $e (1 .. MAX) { + foreach my $r ($e .. MAX) { + # change the door status if employee checks that door + $rooms[$r] *= -1 if ($r % $e == 0); + } +} + +print join ',', grep { $rooms[$_] == 1 } (1 .. MAX);; + +__END__ +./ch-2.pl +1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361,400,441,484 diff --git a/challenge-046/arne-sommer/blog.txt b/challenge-046/arne-sommer/blog.txt new file mode 100644 index 0000000000..92610f39d1 --- /dev/null +++ b/challenge-046/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://raku-musings.com/cryptic-room.html diff --git a/challenge-046/arne-sommer/raku/ch-1.p6 b/challenge-046/arne-sommer/raku/ch-1.p6 new file mode 100755 index 0000000000..076ad913ba --- /dev/null +++ b/challenge-046/arne-sommer/raku/ch-1.p6 @@ -0,0 +1,23 @@ +#! /usr/bin/env raku + +unit sub MAIN ($string is copy = 'Hxl4! ce-lo ze6lg HWlvR q9m#o', :$verbose, :$another); + +$string = 'P+2l!ato 1e80R$4u 5-r]+a>/ Pxwlb3k\ 2e35R8yu <!r^()k0' if $another; + +my @strings = $string.words; +my $max = @strings>>.chars.max; + +say ": Max length: $max" if $verbose; + +my @result; + +for ^$max -> $index +{ + @result.push: @strings.map({ $_.substr($index,1) // "" }).repeated.unique; + + say ": Pos $index : { @strings.map({ $_.substr($index,1) // "" }) }" + ~ " -> { @strings.map({ $_.substr($index,1) // "" }).repeated.unique }" + if $verbose; +} + +say @result.join; diff --git a/challenge-046/arne-sommer/raku/ch-2.p6 b/challenge-046/arne-sommer/raku/ch-2.p6 new file mode 100755 index 0000000000..d9f36d9cc7 --- /dev/null +++ b/challenge-046/arne-sommer/raku/ch-2.p6 @@ -0,0 +1,19 @@ +#! /usr/bin/env raku + +unit sub MAIN (:$verbose); + +my @open; + +for 1 .. 500 -> $employee +{ + print "E: $employee | Rooms:" if $verbose; + + for ($employee, $employee + $employee ... 500) -> $index + { + print " $index" if $verbose; + @open[$index] = ! @open[$index]; + } + say "" if $verbose; +} + +say "Open rooms: { (1..500).grep({@open[$_] }).join(',') }."; diff --git a/challenge-046/arne-sommer/raku/cryptic b/challenge-046/arne-sommer/raku/cryptic new file mode 100755 index 0000000000..37b7efb3fc --- /dev/null +++ b/challenge-046/arne-sommer/raku/cryptic @@ -0,0 +1,49 @@ +#! /usr/bin/env raku + +unit sub MAIN ($string is copy = 'Hxl4! ce-lo ze6lg HWlvR q9m#o', :$verbose, :$another); + +$string = 'P+2l!ato 1e80R$4u 5-r]+a>/ Pxwlb3k\ 2e35R8yu <!r^()k0' if $another; + +my @strings = $string.words; +my $max = @strings>>.chars.max; + +say ": Max length: $max" if $verbose; + +my @result; + +for ^$max -> $index +{ + @result.push: @strings.map({ $_.substr($index,1) // "" }).repeated.unique; + + say ": Pos $index : { @strings.map({ $_.substr($index,1) // "" }) }" + ~ " -> { @strings.map({ $_.substr($index,1) // "" }).repeated.unique }" + if $verbose; +} + +expand("", @result); + +sub expand ($current, @strings) +{ + say ":a: $current | @strings[]" if $verbose; + + my $copy = $current; + + for ^@strings.elems -> $index + { + my $curr = @strings[$index]; + say ":b: $curr at $index" if $verbose; + if $curr.elems > 1 + { + if $verbose { say ":c: $copy | $_" for @$curr } + expand($copy ~ $_, @strings[$index+1 .. Inf]) for @$curr; + return; + } + else + { + say ":d: $curr" if $verbose; + $copy ~= $curr; + } + } + say $copy; +} + diff --git a/challenge-046/arne-sommer/raku/cryptic-simple b/challenge-046/arne-sommer/raku/cryptic-simple new file mode 100755 index 0000000000..076ad913ba --- /dev/null +++ b/challenge-046/arne-sommer/raku/cryptic-simple @@ -0,0 +1,23 @@ +#! /usr/bin/env raku + +unit sub MAIN ($string is copy = 'Hxl4! ce-lo ze6lg HWlvR q9m#o', :$verbose, :$another); + +$string = 'P+2l!ato 1e80R$4u 5-r]+a>/ Pxwlb3k\ 2e35R8yu <!r^()k0' if $another; + +my @strings = $string.words; +my $max = @strings>>.chars.max; + +say ": Max length: $max" if $verbose; + +my @result; + +for ^$max -> $index +{ + @result.push: @strings.map({ $_.substr($index,1) // "" }).repeated.unique; + + say ": Pos $index : { @strings.map({ $_.substr($index,1) // "" }) }" + ~ " -> { @strings.map({ $_.substr($index,1) // "" }).repeated.unique }" + if $verbose; +} + +say @result.join; diff --git a/challenge-046/arne-sommer/raku/room500 b/challenge-046/arne-sommer/raku/room500 new file mode 100755 index 0000000000..d9f36d9cc7 --- /dev/null +++ b/challenge-046/arne-sommer/raku/room500 @@ -0,0 +1,19 @@ +#! /usr/bin/env raku + +unit sub MAIN (:$verbose); + +my @open; + +for 1 .. 500 -> $employee +{ + print "E: $employee | Rooms:" if $verbose; + + for ($employee, $employee + $employee ... 500) -> $index + { + print " $index" if $verbose; + @open[$index] = ! @open[$index]; + } + say "" if $verbose; +} + +say "Open rooms: { (1..500).grep({@open[$_] }).join(',') }."; diff --git a/challenge-046/arne-sommer/raku/room500-infseq b/challenge-046/arne-sommer/raku/room500-infseq new file mode 100755 index 0000000000..e15083134c --- /dev/null +++ b/challenge-046/arne-sommer/raku/room500-infseq @@ -0,0 +1,5 @@ +#! /usr/bin/env raku + +my $open := { ++$ ** 2 } ... *; + +say "Open Rooms: { $open[0 ..^ sqrt(500).Int].join(',') }."; diff --git a/challenge-046/arne-sommer/raku/room500-loop b/challenge-046/arne-sommer/raku/room500-loop new file mode 100755 index 0000000000..d6f6a1ce1d --- /dev/null +++ b/challenge-046/arne-sommer/raku/room500-loop @@ -0,0 +1,22 @@ +#! /usr/bin/env raku + +unit sub MAIN (:$verbose); + +my @open; + +for 1 .. 500 -> $employee +{ + print "E: $employee | Rooms:" if $verbose; + + for ($employee, $employee + $employee ... 500) -> $index + { + print " $index" if $verbose; + @open[$index] = ! @open[$index]; + } + say "" if $verbose; +} + +for 1 .. 500 -> $room +{ + say "Room { $room.fmt('%3d')} is open" if @open[$room]; +} diff --git a/challenge-046/arne-sommer/raku/room500-seq b/challenge-046/arne-sommer/raku/room500-seq new file mode 100755 index 0000000000..52aa93b5f0 --- /dev/null +++ b/challenge-046/arne-sommer/raku/room500-seq @@ -0,0 +1,5 @@ +#! /usr/bin/env raku + +my $open := { ++$ ** 2 } ...^ *>= 500; + +say "Open Rooms: { $open.join(',') }."; diff --git a/challenge-046/cristian-heredia/perl/ch-1.pl b/challenge-046/cristian-heredia/perl/ch-1.pl new file mode 100644 index 0000000000..38762ea171 --- /dev/null +++ b/challenge-046/cristian-heredia/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +#First, we write the message into arrays: +my @array1 = ('P', '+', '2', 'l', '!', 'a', 't', 'o' ); +my @array2 = ('1', 'e', '8', '0', 'R', '$', '4', 'u' ); +my @array3 = ('5', '-', 'r', ']', '+', 'a', '>', '/' ); +my @array4 = ('P', 'x', 'w', 'l', 'b', '3', 'k', '\\' ); +my @array5 = ('2', 'e', '3', '5', 'R', '8', 'y', 'u' ); +my @array6 = ('<', '!', 'r', '^', '(', ')', 'k', '0' ); + +my @arrayCharacter; + +print "The message is: "; +foreach (my $i = 0; $i < @array1; $i++) { + + @arrayCharacter = (); + push(@arrayCharacter, @array1[$i]); + push(@arrayCharacter, @array2[$i]); + push(@arrayCharacter, @array3[$i]); + push(@arrayCharacter, @array4[$i]); + push(@arrayCharacter, @array5[$i]); + push(@arrayCharacter, @array6[$i]); + + my %count; + $count{$_}++ foreach @arrayCharacter; + + #removing the lonely strings + while (my ($key, $value) = each(%count)) { + if ($value == 1) { + delete($count{$key}); + } + } + + #output the counts + while (my ($key, $value) = each(%count)) { + print "$key"; + } +} + + + + |
