Jar Puzzle Problem in Ruby
In puzzle.rb.
require './get_array.rb'
class Puzzle
def initialize(jar_capacity, initial_state, final_states)
#maximum capacities of jars set by the users
@max_jar, @medium_jar, @small_jar = jar_capacity
#initial state configs set by the users
@start_states = initial_state
#final states configs set by the users
@final_states = final_states
@results = Array.new
@visited_memory = Hash.new
end
def display_data
visit_states(@start_states)
puts '=====Your Results with sequences====='
if !@results.empty?
@results.reverse.collect {|x| p x}
else
puts 'No Path Found'
end
end
def visit_states(jar_states)
jar_a, jar_b, jar_c = jar_states
#compared with final states given by the users
if(jar_a == @final_states[0] && jar_b == @final_states[1])
@results.push(jar_states)
return true
end
#check the states in already visited
if @visited_memory.key?([jar_a, jar_b, jar_c])
return false
end
@visited_memory[[jar_a, jar_b, jar_c]] = 1
#empty the jar_a
if(jar_a > 0)
#empty jar_a into jar_b
if(jar_a + jar_b <= @medium_jar)
if( visit_states([0,jar_a + jar_b,jar_c]) )
@results.push(jar_states)
return true
end
else
if( visit_states([jar_a - (@medium_jar - jar_b), @medium_jar, jar_c]) )
@results.push(jar_states)
return true
end
end
#empty jar_a into jar_c
if(jar_a + jar_c <= @small_jar)
if( visit_states([0,jar_b,jar_a + jar_c]) )
@results.push(jar_states)
return true
end
else
if( visit_states([jar_a-(@small_jar - jar_c), jar_b, @small_jar]) )
@results.push(jar_states)
return true
end
end
end
#empty the jar_b
if(jar_b > 0)
#empty jar_b to jar_a
if(jar_a + jar_b <= @max_jar)
if( visit_states([jar_a + jar_b, 0, jar_c]) )
@results.push(jar_states)
return true
end
else
if( visit_states([@max_jar, jar_b - (@max_jar - jar_a), jar_c]) )
@results.push(jar_states)
return true
end
end
#empty jar_b into jar_c
if(jar_b + jar_c <= @small_jar)
if( visit_states([jar_a, 0, jar_b + jar_c]) )
@results.push(jar_states)
return true
end
else
if( visit_states([jar_a, jar_b - (@small_jar - jar_c), @small_jar]) )
@results.push(jar_states)
return true
end
end
end
#empty jar_c
if(jar_c > 0)
#empty jar_c into jar_a
if(jar_a + jar_c <= @max_jar)
if( visit_states([jar_a + jar_c, jar_b, 0]) )
@results.push(jar_states)
return true
end
else
if( visit_states([@max_jar, jar_b, jar_c - (@max_jar - jar_a)]) )
@results.push(jar_states)
return true
end
end
#empty jar_c into jar_b
if(jar_b + jar_c <= @medium_jar)
if( visit_states([jar_a, jar_b + jar_c, 0]) )
@results.push(jar_states)
return true
end
else
if( visit_states([jar_a, @medium_jar, jar_c-(@medium_jar - jar_b)]) )
@results.push(jar_states)
return true
end
end
end
return false
end
end
puts "=============Please enter jar capacities e.g. 8,5,3================"
total_capacities = GetArray.convert_array(gets.chomp)
puts "=============Please enter Initial configuration of jars e.g.=======8,0,0=="
initial_states = GetArray.convert_array(gets.chomp)
puts "=============Please enter the final configuration of jars==== e.g. 4,4,0========"
final_states = GetArray.convert_array(gets.chomp)
puzzle = Puzzle.new(total_capacities, initial_states, final_states)
puzzle.display_data
In get_array.rb
class GetArray
def self.convert_array(inputs)
if inputs != ''
begin
if inputs.split(',').map(&:to_i).size >=3
inputs.split(',').map(&:to_i)
else
raise 'Your input should be minimum three values'
exit(1)
end
rescue Exception => e
puts e.message
end
end
end
end