Task 18.2 - echo_string/medium.rb

This commit is contained in:
thomashaw
2018-08-14 08:37:14 +01:00
committed by ts
parent db92817275
commit fc91f9c78e
3 changed files with 45 additions and 3 deletions

View File

@@ -1,3 +1,8 @@
# #####################
# Programming Challenge
# Echo string - Repeat a string and repeat back to the script
# #####################
require 'securerandom'
require 'timeout'

View File

@@ -0,0 +1,37 @@
# #####################
# Programming Challenge
# Echo string - Medium: Reverse a string and repeat it back to the script
# #####################
require 'securerandom'
require 'timeout'
$stdout.sync = true # Disable stdout caching (for challenges that can be run over the network)
puts 'Prepare yourself. You need to work quickly for this challenge.'
sleep 2
puts 'Echo the string back to me, in reverse, before the script times out. Try writing a script of your own to make it easier!'
sleep 2
puts 'Get ready, here we go...'
sleep 2
# Generate random string
string = [SecureRandom.base64(rand(20..40)), SecureRandom.hex(rand(20..40))].sample
puts string
STDOUT.flush
begin
Timeout.timeout 5 do
response = gets.chomp
if response == string.reverse
puts File.read('flag')
else
puts 'Incorrect answer!'
exit
end
end
rescue Timeout::Error
puts 'Too slow!'
exit
end

View File

@@ -18,9 +18,9 @@
<read_fact>difficulty</read_fact>
<default_input into="difficulty">
<value>low</value>
<!--TODO: implement higher difficulty options -->
<!--<value>medium</value>-->
<!--<value>low</value>-->
<value>medium</value>
<!--TODO: implement high difficulty options -->
<!--<value>high</value>-->
</default_input>