Created
July 1, 2012 11:59
-
-
Save sairam/3028162 to your computer and use it in GitHub Desktop.
Hacker Rank Script to automate submission
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Hacker Rank at https://www.hackerrank.com/ | |
# Hacker rank Candies game was Puzzle 1 released on 30 June 2012 . | |
# If you are using Ruby 1.8 . make sure you install json . Ruby 1.9 comes with json. | |
csrf="SET_THIS" | |
cookie="_hackerrank_session=SET_THIS_AS_WELL" | |
mkdir answers | |
function startGame() { | |
curl -s -D header -o output -X POST -H "X-CSRF-Token:$csrf" -H "X-Requested-With:XMLHttpRequest" -H "Cookie: $cookie" --data "n=$1" https://www.hackerrank.com/splash/challenge.json | |
val=`getCurrent output` | |
playGame `expr $val % 6` `getGameId header` $1 | |
} | |
function playGame() { | |
curl -s -D header -o output -X PUT -H "X-CSRF-Token:$csrf" -H "X-Requested-With:XMLHttpRequest" -H "Cookie: $cookie;game=$2" --data "move=$1" https://www.hackerrank.com/splash/challenge.json | |
if [[ `getGameExited output` == "0" ]] | |
then | |
echo "Game over" | |
moved=`cat moved` | |
gameId=`getGameId header-last` | |
echo $gameId > answers/$3-$moved | |
# game completed, start next | |
else | |
# continue game | |
val=`getGameCurrent output` | |
/bin/cp -f header header-last | |
echo $val | |
toMove=`expr $val % 6` | |
echo $toMove > moved | |
echo "Picking $toMove" | |
playGame $toMove `getGameId header` $3 | |
fi | |
} | |
function getGameId() { | |
grep "Set-Cookie: game" $1 | cut -f1 -d";"|cut -f2 -d"=" | |
} | |
function getCurrent() { | |
ruby_cmd="require 'rubygems'; require 'json'; puts JSON.parse(File.read(\"$1\"))['current']" | |
ruby -e "$ruby_cmd" | |
} | |
function getGameCurrent() { | |
ruby_cmd="require 'rubygems'; require 'json'; puts JSON.parse(File.read(\"$1\"))['game']['current']" | |
ruby -e "$ruby_cmd" | |
} | |
function getGameExited() { | |
ruby_cmd="require 'rubygems'; require 'json'; puts JSON.parse(File.read(\"$1\"))['exit']" | |
ruby -e "$ruby_cmd" | |
} | |
# for i in `seq 7 2560` | |
# do | |
# if [[ `expr $i % 6` -ne 0 ]] | |
# then | |
# echo $i | |
# startGame $i | |
# fi | |
# done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Hacker Rank at https://www.hackerrank.com/ | |
# Hacker rank Candies game was Puzzle 1 released on 30 June 2012 . | |
startRequest = (number) -> | |
$.ajax | |
url: '/splash/challenge.json' | |
data: | |
n: number | |
remote: 'true' | |
type: 'POST' | |
success: (data) -> | |
makeIntelligentMove(data.current) | |
true | |
makeIntelligentMove = (number) -> | |
# Actual logic to determine the number to move | |
numberToMove = number % 6 # we use 6 since 5 is the maximum number to pick | |
$.ajax | |
url: '/splash/challenge.json' | |
data: | |
move: numberToMove | |
remote: 'true' | |
type: 'PUT' | |
success: (data) -> | |
console.log(data.message) | |
unless data.exit == 0 | |
makeIntelligentMove(data.game.current) | |
else | |
$.ajax | |
url: 'splash/userstats.json' | |
success: (data) -> | |
console.log('Your score is '+data.score) | |
window.currentIndex++ | |
if window.successCalls.length > window.currentIndex | |
setTimeout(startRequest, 1000, window.successCalls[window.currentIndex]) | |
window.currentIndex = 0 | |
window.successCalls = [7]; # (6..2560).to_a.delete_if{|x| x%6 == 0 } # count is 2129 . | |
setTimeout(startRequest, 1000, window.successCalls[window.currentIndex]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright (C) 2012, Sairam Kunala | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Hacker Rank at https://www.hackerrank.com/ | |
# Hacker rank SpaceX game was Puzzle 2 released on 3 July 2012. | |
# CheatSheet - The answers vary per user unlike the Candies game. So you cannot use one set of answers for another user if you have any or planning to cheat :) | |
# README - The Shell script | |
# Mind you since this will blow up your CPU and memory depending on the no of parallel jobs you run compared to a single thread of curl. | |
# With 30 threads on a 512 MB RAM Virt using up 90% memory, it took the memory towards 150% and CPU util from 0% to 400%. | |
# Process started at Wed Jul 4 12:22:00 IST 2012. | |
# Process ended at Wed Jul 4 12:30:10 IST 2012. | |
csrf="SET_THIS" | |
cookie="_hackerrank_session=SET_THIS_AS_WELL" | |
noOfThreads=30 # Change this as per your Compute Engines aka CPU | |
startQ=1 | |
endQ=10000 | |
answers="answers.txt" | |
function startDownload { | |
func=$1 | |
startQ=$2 | |
endQ=$3 | |
noOfThreads=$4 | |
for i in `seq $startQ $endQ` ; do | |
while [ 1 ]; do | |
if [[ `jobs | wc -l` -gt $noOfThreads ]]; then | |
sleep 1 | |
else | |
break | |
fi | |
done | |
if [[ -f stop ]]; then | |
# touch the file from another shell into this directory in order to stop the executions | |
break | |
end | |
echo $i | |
$func $i & | |
done | |
} | |
function getQuestion { | |
i=$1 | |
curl -s -o q-$i -X POST --data "n=$i" -H "X-CSRF-Token:$csrf" -H "X-Requested-With:XMLHttpRequest" -H "Cookie: $cookie" https://www.hackerrank.com/game.json | |
} | |
function submitAnswers { | |
answers=$1 | |
for i in `cat $answers` do | |
splitValueAndSubmit $i | |
done | |
} | |
function splitValueAndSubmit { | |
i=$1 | |
q=`echo $i|cut -f1 -d,` | |
a=`echo $i|cut -f2 -d,` | |
curl -s -X PUT -H "X-CSRF-Token:$csrf" -H "X-Requested-With:XMLHttpRequest" -H "Cookie: $cookie" --data "answer=$a&id=$q" https://www.hackerrank.com/game.json | |
} | |
mkdir spacex ; cd spacex | |
# Part 1 - Downloading questions | |
startDownload getQuestion $startQ $endQ $noOfThreads | |
# Part 2 - Run the ruby script to get the awesome answers | |
ruby ../spacex-figureout-text.rb | grep -v gnoring > $answers | |
# Part 3 - The final - Submit the data | |
# This is the part 3 of winning the game ( Make sure you see part 2 and run it ) | |
submitAnswers $answers | |
cd .. | |
echo "You did great! That's a lot of effort to run a shell script" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
$az = ('a'..'z').to_a | |
$cardinals = { | |
"one" => 1 , | |
"two" => 2 , | |
"three" => 3 , | |
"four" => 4 , | |
"five" => 5 , | |
"six" => 6 , | |
"seven" => 7 , | |
"eight" => 8 , | |
"nine" => 9 , | |
"ten" => 10 , | |
"eleven" => 11 , | |
"twelve" => 12 , | |
"thirteen" => 13 , | |
"fourteen" => 14 , | |
"fifteen" => 15 , | |
"sixteen" => 16 , | |
"seventeen"=> 17 , | |
"eighteen" => 18 , | |
"nineteen" => 19 , | |
"twenty" => 20 , | |
"thirty" => 30 , | |
"fourty" => 40 , | |
"forty" => 40 , | |
"fifty" => 50 , | |
"sixty" => 60 , | |
"seventy" => 70 , | |
"eighty" => 80 , | |
"ninety" => 90 , | |
"hundred" => 100 , | |
"thousand" => 1_000 , | |
"and" => 0, | |
"zero" => 0, # there was some guy who thought of this | |
"million" => 1_000_000 , # and none who thought of this | |
} | |
def chipher(str,diff) | |
az = $az | |
a = str.split('').map do |c| | |
if k = az.find_index(c) | |
az[(k + diff) % 26] | |
else | |
' ' | |
end | |
end | |
a.join('') | |
end | |
# number_to_human is the method in rails/active support , this does not work for three hundred thousand. will need to fix this | |
def to_bot(str) | |
str.split(" ").map{ |n| $cardinals[n] }.inject(0){|r,i| i >=100 ? r/100*100 + (r%100 * i) : r/100*100 + (r%100 +i) } | |
end | |
def diff_char(a,b) | |
az = $az | |
(az.index(a) - az.index(b)) % 26 | |
end | |
def try_find_the_word(s) | |
az = $az | |
s.split(/[ ,-]/).each do |w| | |
$cardinals.keys.each do |ck| | |
success = true | |
next if ck.length != w.length | |
constant = diff_char(ck[0], w[0]) | |
(ck.length - 1).times do |i| | |
unless (az.index(ck[i+1]) - az.index(w[i+1])) % 26 == constant | |
success = false | |
break | |
end | |
end | |
success == false ? next : (words << ck) and break | |
end | |
end | |
end | |
# list files | |
Dir.new(".").to_a.each do |file| | |
next unless file =~ /q-[0-9]/ # local file names | |
data = File.read(file) | |
json = JSON.parse(data) | |
q = json['game']['fact_question'].sub(/Who is the author of the book / , '' ).sub('?', '') | |
c = json['game']['fact_cph_answer'] | |
s = json['game']['cph_number'] | |
id = json['game']['id'] | |
# figure out the thousand if not . figure out hundred | |
a = s.split(/[ ,-]/).keep_if{|x| x.length == 8 && x[-3] != x[-2]} # fix for teen | |
type = :thousand | |
if a.size <= 0 | |
a = s.split(/[ ,-]/).keep_if{|x| x.length == 7 && x[-4] == x[-1] } # d = d in hundred | |
type = :hundred | |
end | |
if a.size > 0 | |
d = case type | |
when :thousand | |
a[0].ord - 't'.ord | |
when :hundred | |
a[0].ord - 'h'.ord | |
end | |
begin | |
str = chipher(s,-d) | |
puts "#{id},#{to_bot(str)}" | |
rescue | |
# for bad input, You never know | |
puts "Cant decipher - #{id} #{str} with cipher #{s}" | |
end | |
# detect number | |
else | |
# worse case scenario | |
# detect based on the words and translate up everything | |
words = try_find_the_word(s) | |
if words.size > 0 | |
puts "#{id},#{to_bot(words.join(' '))}" | |
else | |
type = :ignore | |
puts "Ignoring #{id} - #{file}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment