Skip to content

Instantly share code, notes, and snippets.

@crossbone-magister
Last active August 4, 2019 15:56
Show Gist options
  • Save crossbone-magister/cb4b182bdaac98424f0d411466663b57 to your computer and use it in GitHub Desktop.
Save crossbone-magister/cb4b182bdaac98424f0d411466663b57 to your computer and use it in GitHub Desktop.
Final Fantasy The Four Heroes of Light Math Minigame Solver
#--------------
# This code was written some time ago to solve the Math Minigame in the game Final Fantasy The Four Heroes of Light that allows the player to unlock the Beastmaster Crown.
# To use the script run 'ruby ff4hl-math-solver.rb <number1> <number2> <number3> <number4>'.
# The script will output the combination that awards the most points
#--------------
def find_best(operators,numbers)
all_operators_perm = operators.repeated_permutation(3).to_a
all_numbers_perm = numbers.permutation.to_a
res = []
all_numbers_perm.each do |num_quad|
all_operators_perm.each do |op_quad|
start = num_quad[0]
others = num_quad.slice(1,num_quad.length-1)
res << op_quad.inject({:sequence => start, :points => 0, :result => start, :numbers => others, :valid => true}) do |memo,op|
current_number = memo[:numbers].shift
new_val = eval(memo[:result]+op+current_number)
case (op)
when '/'
memo[:valid] = eval(memo[:result]+'%'+current_number) == 0
when '-'
memo[:valid] = new_val.to_i < 0 && memo[:valid]
end
memo[:sequence] += op + current_number + ';' + new_val.to_s
memo[:points] += new_val
memo[:result] = new_val.to_s
memo
end
end
end
cor_seq = res.select do |element|
element[:result].to_i == 10 && element[:valid]
end.max do |e1,e2|
e1[:points] <=> e2[:points]
end
if cor_seq != nil && !cor_seq.empty?
return "Best combination: #{cor_seq[:sequence]} for #{cor_seq[:points]} points"
else
return "No available combinations"
end
end
operators = ['+','-','*','/']
numbers = ARGV[0..3]
puts find_best(operators,numbers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment