######################
#  LEAVE THIS TOP PORTION INTACT
require_relative 'turing_machine'

unless ARGV.size == 1
  STDERR.puts "Usage: simulate_turing_machine <input string>"
  exit(-1)
end

# convert string into list of symbols
input_string = ARGV.first.split(//).map{ |ch| ch.intern }

machine = TuringMachine.new
machine.start_state = :start
machine.accepting_states << :finish

# define trace map
map = {
  :blank => '.',
  :B     => 'b',
  :s    => 's',
  :l     => '1',
  :o     => '0',
  :L     => 'L',
  :R     => 'R'
}
#CONVERTER: accepts any inputs in the form BBxxxByyyBzzz 
# and returns ..xxx.yyy.zzz with head at first word

machine.transitions[[ :start, :B    ]] = [ :blank, :start ]
machine.transitions[[ :start, :blank ]] = [:R, :statei]

machine.transitions[[ :statei, :B ]] = [:s, :statej]


machine.transitions[[ :statej, :B    ]] = [ :R, :statej ]
machine.transitions[[ :statej, :s    ]] = [ :R, :statej ]
machine.transitions[[ :statej, :o    ]] = [ :R, :statej ]
machine.transitions[[ :statej, :l   ]] = [ :R, :statej ]
machine.transitions[[ :statej, :blank    ]] = [ :L, :statek ]

machine.transitions[[ :statek, :o ]] = [ :L, :statek ]
machine.transitions[[ :statek, :l ]] = [ :L, :statek ]
machine.transitions[[ :statek, :s ]] = [ :blank, :statem ]
machine.transitions[[ :statek, :B ]] = [ :blank, :statel ]

machine.transitions[[ :statel, :blank ]] = [ :L, :statek ]

machine.transitions[[ :statem, :blank    ]] = [ :R, :state1]

####################################

#YOUR PROGRAM STARTS HERE WITH THE INITIAL STATE 1


#The successor function

#if solid llls, shift right; leave head at right end
machine.transitions[[ :state1, :o ]] = [ :R, :state4 ]
machine.transitions[[ :state1, :l ]] = [ :R, :state1 ]
machine.transitions[[ :state1, :blank ]] = [ :l, :state2 ]

machine.transitions[[ :state2, :l ]] = [ :L, :state2 ]
machine.transitions[[ :state2, :blank ]] = [ :R, :state3 ]
 
machine.transitions[[ :state3, :l ]] = [ :blank, :state3 ]
machine.transitions[[ :state3, :blank ]] = [ :R, :state4 ]

machine.transitions[[ :state4, :o ]] = [ :R, :state4 ]
machine.transitions[[ :state4, :l  ]] = [ :R, :state4 ]
machine.transitions[[ :state4, :blank ]] = [ :L, :state5 ]

#Moving from right, flip 1 to 0 until get to 0 and flip to 1
machine.transitions[[ :state5, :o ]] = [ :l, :state7 ]
machine.transitions[[ :state5, :l  ]] = [ :o, :state6 ]
machine.transitions[[ :state5, :blank  ]] = [ :l, :state7 ]

machine.transitions[[ :state6, :o ]] = [ :L, :state5 ]

#Move to left end
machine.transitions[[ :state7, :o ]] = [ :L,  :state7 ]
machine.transitions[[ :state7, :l  ]] = [ :L,  :state7 ]
machine.transitions[[ :state7, :blank ]] = [ :R,  :finish ]

###############################
# LEAVE THIS PORTION INTACT
# run simulation
machine.simulate(input_string, map)



