An π easy π» but π¬ spooky π Ruby challenge
This was a challenge in the Ruby Discord server. The contents of the challenge is:Β©
Halloween Challenge
Itβs the weekend and youβve just completed a seance with friends. After communing with the dead, you realize a mysterious message was left behind.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 3π¬4π04π6π»00π62π6π»32π»5π4π¬42π4π2π6π¬3π52π¬3π6π0π2π6π¬13π¬0π432π»4π»4π230π62π1π¬03π2π¬6 π¬4π»5π»3π220π5π»0π»5π4π6π»42π4π»01π60π¬1π2π»3π»30π6π0π»0π¬3π5π»0π»5π6π»0π¬30π61π¬0π1π2π6π42π»3π¬03π2π3 π0π2π»5π22π¬3π5π6π¬3π5π2π6π52π¬4π»5π¬3π2π1π6π»4π¬0π¬0π»5π¬6π6π»0π¬30π604π»5π¬32π1π6π0π2π6π2π1π¬1π»3π¬03π2π¬6 4π2π¬3π6π»0π»5π6π¬3π5π2π6π0π¬03π»3π1π6π¬0π3π6π¬13π¬0π432π»4π»4π»0π»5π4π¬6π6π»0π¬3π6π¬3π53π»0π¬5π20π6π2π¬5π2π»5π6π»4π¬03π2π3 π»0π»5π6π¬3π5π2π6134π¬1π¬3π»01π6π»4π2π¬3π5π¬0π10π¬6π6π0π5π23π2π6π2π23π»0π2π6π0π¬4π40π6π»424π6π¬33π22π1π¬6 0π»2π2π»3π2π¬3π¬0π»50π6π¬0π3π6π233π¬030π6π»0π»5π6π¬3π5π2π6π123π»2π623π2π6π0π»0π1π20π¬13π22π1π3 π»5π¬0π¬3π62π6π»32π»5π4π¬42π4π2π6π¬0π3π6π¬3π5π2π6π¬120π¬3π¬6π6π0π¬4π¬3π6π¬0π»5π2π6π0π2π6π5π¬0π»3π1π6π¬0π¬43π6π5π22π10π¬6 π¬0π¬5π23π61π¬0π¬4π»5π¬3π»3π200π6π¬13π¬0π»1π21π¬30π¬6π6π0π5π23π2π6π»0π¬30π6π¬1π¬0π0π23π6π520π60π¬13π22π1π3 π¬3π5π2π6π¬33π»01π»20π62π»5π1π6π¬33π22π¬30π6π¬0π3π63π¬4π04π¬6π6π»3π»0π»2π2π6π0π»0π¬31π5π20π»6π61π523π»40π¬6π61π¬0π»5π¬5π2π»5π2π¬6 π132π0π»0π»5π4π6π¬40π6π»0π»5π¬3π¬0π6π»0π¬30π6π0π¬03π»3π1π¬6π6π0π5π23π2π6π¬3π5π2π60π¬4π0π»3π»0π»4π2π6π»00π60π2π2π»5π3 π2π»51π52π»5π¬3π»0π»5π4π6π¬40π6π0π»0π¬3π5π6π4π2π»40π¬6π6π»0π¬3π»60π6π2π¬5π234π61π¬0π1π23π»60π6π13π22π»4π¬6 2π1π¬5π2π»5π¬3π¬43π20π6π»0π»5π6π¬3π5π2π61π¬0π1π2π¬6π6π0π5π23π2π6π¬3π5π2π6π2π23π»0π2π»60π63π¬0π¬4π¬3π»0π»5π2π3 π1π22π¬3π5π6π»424π60π2π2π»4π6π¬3π¬0π6π»3π¬43π»2π¬6π6π0π¬4π¬3π6π3π¬03π63π¬4π04π6π»0π¬3π»60π6π52π»3π»3π¬0π0π2π2π»56However, with your unique Ouija board you should have no problem deciphering what they left!
Objective
Your Ouija board looks like the following straddling checkerboard:
1 2 3 4 5 6 7 8 ================================== | | 0 | 1 | 2 | 3 | 4 | 5 | 6 | | | S | C | A | R | Y | ? | ! | | π | B | D | E | F | G | H | | | π» | I | J | K | L | M | N | ' | | π¬ | O | P | Q | T | U | V | , | | π | W | X | Z | . | # | $ | : | ==================================Use your Ruby skills and the board above to decrypt the message. I have attached a file to help get you started. You donβt need to use it if you donβt want to.
You may also find this link helpful too.
Requirements
- Must use Ruby
- Decrypt the message
- Determine the hidden message within the decrypted message
My solution
I am too stupid to think of regular expressions at first, so I wrote this:
1 2 3 |
" 0123456\n SCARY?!\nπBDEFGH \nπ»IJKLMN'\nπ¬OPQTUV,\nπWXZ.\#$:".split(?\n).map(&:chars).tap{|b|<<M.chars.reduce(nil){|r,e|e==?\n?print(e): r ?print(b[r][b[0].index e]): b.index{_1[0]==e}||print(b[1][b[0].index e])}}
3π¬4π04...
M
|
I did not want to code golf, but I did intend to wrote a one-liner. It seems hard to understand, but it is pretty straightforward if it is expanded:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
board = [' ', '0', '1', '2', '3', '4', '5', '6'],
[' ', 'S', 'C', 'A', 'R', 'Y', '?', '!'],
['π', 'B', 'D', 'E', 'F', 'G', 'H', ' '],
['π»', 'I', 'J', 'K', 'L', 'M', 'N', "'"],
['π¬', 'O', 'P', 'Q', 'T', 'U', 'V', ','],
['π', 'W', 'X', 'Z', '.', '#', '$', ':']
message = <<MESSAGE
3π¬4π04...
MESSAGE
message.chars.reduce nil do |row, encoded_char|
if encoded_char == ?\n # newline in the message
print encoded_char
elsif row # last char is an emoji, corresponding to a row in the board
print board[row][board[0].index encoded_char]
nil
elsif new_row = board.index { _1[0] == encoded_char }
new_row
else
print board[1][board[0].index encoded_char]
nil
end
end
|
Then, I realized that I could have used regular expressions, so I wrote a cleaner version:
1 2 3 |
puts <<M.gsub(/([ππ»π¬π])?([0-6])/){|s|{nil=>'SCARY?!',π:'BDEFGH ',π»:"IJKLMN'",π¬:'OPQTUV,',π:'WXZ.#$:'}[$1&.to_sym][$2.to_i]}
3π¬4π04...
M
|
Then, I suddenly become creative and realized that I can use another regular expression to implement a string-based indexing, and that I can use -p
option of Ruby command line to save even more characters (here I smelled code golfing):
1 2 |
#!/usr/bin/env ruby -p
gsub(/(\D?)(\d)/){'SCARY?!πBDEFGH π»IJKLMN\'π¬OPQTUV,πWXZ.#$:'[/#$1.{#$2}(.)/,1]}
|
Here are some other solutions. Check them out!
Some explanations for the code golf solution
- The
-p
option basically wraps the code in awhile gets
loop, and you can access the current line with$_
. Ruby will output the contents of$_
after each iteration. - The method
Kernel#gsub
modifies$_
(the current processing input line). It is only available when running Ruby with-p
option. - The method
String#[]
returns a substring. What is good about this method is that, if you use a regular expression to find the substring, you can use the second argument to specify which capture group in the regular expression you want to return. - In a string literal, you can use
#$some_global_variable
as a shortcut of#{$some_global_variable}
. This is also true for instance variables and class variables.
The message
The decoded message is:Β©
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
RUBY IS A LANGUAGE THAT WE PROGRAMMERS ADORE,
UNLEASHING MAGIC SPELLS WITHIN ITS CODE GALORE.
BENEATH THE HAUNTED MOON, ITS SYNTAX WE EXPLORE,
YET IN THE WORLD OF PROGRAMMING, IT THRIVES EVEN MORE.
IN THE CRYPTIC METHODS, WHERE EERIE BUGS MAY TREAD,
SKELETONS OF ERRORS IN THE DARK ARE WIDESPREAD.
NOT A LANGUAGE OF THE PAST, BUT ONE WE HOLD OUR HEADS,
OVER COUNTLESS PROJECTS, WHERE ITS POWER HAS SPREAD.
THE TRICKS AND TREATS OF RUBY, LIKE WITCHES' CHARMS, CONVENE,
DRAWING US INTO ITS WORLD, WHERE THE SUBLIME IS SEEN.
ENCHANTING US WITH GEMS, IT'S EVERY CODER'S DREAM,
ADVENTURES IN THE CODE, WHERE THE EERIE'S ROUTINE.
DEATH MAY SEEM TO LURK, BUT FOR RUBY IT'S HALLOWEEN!
|
Did you spot the hidden message?