Skip to content

Instantly share code, notes, and snippets.

@shireeshj
Forked from roryokane/.rbenv-version
Created March 29, 2013 06:00
Show Gist options
  • Save shireeshj/5269017 to your computer and use it in GitHub Desktop.
Save shireeshj/5269017 to your computer and use it in GitHub Desktop.
# encoding: utf-8
# for http://adventure.cueup.com/
def roulette
def lcg_next(previous, modulus, multiplier, increment)
((multiplier*previous) + increment) % modulus
end
seed = 6 # use the VAX result as the next seed, not the roulette
m_mod = 2 ** 32
a_multiplier = 69069
c_increment = 1
vax_result = lcg_next(seed, m_mod, a_multiplier, c_increment)
puts "VAX: " + vax_result.to_s
roulette_answer = vax_result % 36
puts "roulette: " + roulette_answer.to_s
end
roulette
def bugs_rabbit
def index_of_balance_error(brackets_string)
index_of_balance_error_recursive(brackets_string.split(//), [], 0)
end
def index_of_balance_error_recursive(brackets_remaining, opening_bracket_stack, index)
opening_brackets = ['{', '[', '(']
closing_brackets = ['}', ']', ')']
closing_bracket_for_opener = {'{'=>'}','['=>']','('=>')'}
trace = false
if trace
puts
p [brackets_remaining, opening_bracket_stack, index]
end
if brackets_remaining.size == 0 # base case
# I could check that the open bracket stack is not full here,
# but that’s not necessary for this problem
return nil
else
this_bracket, other_brackets =
brackets_remaining[0], brackets_remaining[1..-1]
if opening_brackets.include?(this_bracket)
index_of_balance_error_recursive(other_brackets, opening_bracket_stack+ [this_bracket], index+1)
elsif closing_brackets.include?(this_bracket)
top_opening_bracket = opening_bracket_stack[-1]
if this_bracket != closing_bracket_for_opener[top_opening_bracket]
return index
else
index_of_balance_error_recursive(other_brackets, opening_bracket_stack[0...-1], index+1)
end
else
raise "input ‘#{this_bracket}’ is not a bracket"
end
end
end
input_brackets = '{]'
result = index_of_balance_error(input_brackets)
puts "input: " + input_brackets
if result
puts "error at index " + result.to_s
else
puts "no closing balance error"
end
end
# bugs_rabbit
def troll_map
# solved by hand
# just note that you can double back
# my solution was north, east, east, north, east, east, west, north, north, east
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment