Skip to content

Instantly share code, notes, and snippets.

@kevincolyer
Created February 5, 2020 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevincolyer/cc381395709d8797cf85647ad6b4fbe3 to your computer and use it in GitHub Desktop.
Save kevincolyer/cc381395709d8797cf85647ad6b4fbe3 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl6
use v6;
use Test;
=begin pod
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.
=end pod
# ok, it seems that if you look vertically down each column the there are two repeated characters. This marks the first letter (and so on) so message one is "Hello" and 2nd is "PerlRaku"
# so let's write a script for that!
my $message = 'H x l 4 !
c e - l o
z e 6 l g
H W l v R
q 9 m # o';
is unscramble($message),"Hello", "unscrambles ok";
$message='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';
is unscramble($message),"PerlRaku", "unscrambles ok";
sub unscramble($m) {
my %seen;
my $unscramble="";
my @m;
# make a 2d array
# split by lines into an array
# split lines on words (split by spaces) and coerce to a list then push into array
for $m.lines { @m.push( .words.list ) };
# iterate through the columns using elems of row 0 as limit
for ^@m[0].elems -> $c {
my %seen;
# count the number of seen letters on each row of selected column
%seen{ @m[$_][$c] }++ for ^@m.elems;
# filter the seen hash to get the key value of the item with count 2 and add to unscrambled message
$unscramble~=%seen.grep( *.value==2)>>.key;
}
return $unscramble;
}
#!/usr/bin/perl6
use v6;
use Test;
=begin pod
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.
=end pod
# initialise doors to open (= 1) after 1st employee opens all doors
my Int @doors = 1 xx 500;
# loop through employees
for 2..500 -> $e {
my $i=0;
# iterate through doors
while $i < 500 {
@doors[$i] = 1 - @doors[$i];
$i+=$e;
}
}
# reduce array by sum of open doors
say @doors.sum;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment