diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-02-09 03:13:06 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-09 03:13:06 +0000 |
| commit | 0df61b2c8dc6e587295c36bbe0e6ecf5dd3ce81d (patch) | |
| tree | 7897c1dba0491c2a7b37702a5b46365f3cdb6784 | |
| parent | 4b5c60af20136da48c7aea12475c195bf7b0a7ea (diff) | |
| parent | 5d1deb3a8473169bb1675b95fd2c1b1104448d6f (diff) | |
| download | perlweeklychallenge-club-0df61b2c8dc6e587295c36bbe0e6ecf5dd3ce81d.tar.gz perlweeklychallenge-club-0df61b2c8dc6e587295c36bbe0e6ecf5dd3ce81d.tar.bz2 perlweeklychallenge-club-0df61b2c8dc6e587295c36bbe0e6ecf5dd3ce81d.zip | |
Merge pull request #1225 from noudald/challenge-046-noud
Challenge 046 noud
| -rw-r--r-- | challenge-046/noud/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-046/noud/raku/ch-1.p6 | 55 | ||||
| -rw-r--r-- | challenge-046/noud/raku/ch-2.p6 | 41 |
3 files changed, 97 insertions, 0 deletions
diff --git a/challenge-046/noud/blog.txt b/challenge-046/noud/blog.txt new file mode 100644 index 0000000000..d1fa9a7ad0 --- /dev/null +++ b/challenge-046/noud/blog.txt @@ -0,0 +1 @@ +https://www.noudaldenhoven.nl/wordpress/?p=288 diff --git a/challenge-046/noud/raku/ch-1.p6 b/challenge-046/noud/raku/ch-1.p6 new file mode 100644 index 0000000000..8d77c8ade8 --- /dev/null +++ b/challenge-046/noud/raku/ch-1.p6 @@ -0,0 +1,55 @@ +# 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. + +sub select-double(@a) { + for @a -> $elm { + if (@a.grep($elm).elems == 2) { + return $elm; + } + } +} + +sub decrypt(@a) { + return [~] ([Z] @a).map({ select-double($_) }); +} + +my @enc1 = ( + <H x l 4 !>, + <c e - l o>, + <z e 6 l g>, + <H W l v R>, + <q 9 m # o>); + +say decrypt(@enc1); + + +my @enc2 = ( + ('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')); + +say decrypt(@enc2); diff --git a/challenge-046/noud/raku/ch-2.p6 b/challenge-046/noud/raku/ch-2.p6 new file mode 100644 index 0000000000..85131bb6c6 --- /dev/null +++ b/challenge-046/noud/raku/ch-2.p6 @@ -0,0 +1,41 @@ +# 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. + + +# This took me a while to figure out. +# +# 1. Given a single room number N, how many times did an employee open or +# closed the door of that room? Let's look at the k-th employee. The employee +# opens or closes the door of room N if and only if k is a divisor of N. So if +# we want to know how often the door has been opened or closed we must count +# the number of divisors of the room number N. +# +# 2. If room number N has been opened or closed an even number of times the +# door is closed. If the number of divisors of N is an odd number the door is +# open. So this problem is equivalent to finding all N equal or below 500 that +# have an odd number of divisors. +# +# 3. Take the prime decomposition of N: +# +# N = p1^k1 * p2^k2 * ... * pi^ki. +# +# The number of divisors of N is (k1 + 1) * (k2 + 1) * ... * (ki + 1). +# +# 4. The number of divisors of N is odd if and only if k1, k2, ..., ki, are all +# even. (If kj would be odd, kj + 1 is even, hence every product with kj + 1 is +# even) But if k1, k2, ..., ki are all even, N is a squared number. +# +# 5. Therefore, the only open rooms can be rooms with a squared number below +# 500. This is a (maybe difficult to understand why correct) one-liner. + +say "Open rooms:"; +say $_**2 for 1..(500.sqrt); |
