Guest User

Untitled

a guest
Jun 13th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. ## Detailed beginner solution ##
  2. # Number of road patterns
  3. n = gets.to_i
  4. steering_commands = gets.chomp.split(';')
  5. # Car position on the road first position is 1 and not 0
  6. # That's why we need to substract 1
  7. car_pos=steering_commands.shift.to_i-1
  8. # We will build an array of all the steering instructions to apply
  9. steering_per_turn=[]
  10. steering_commands.each do |command_block|
  11. # Steering instruction is the last letter of the command block
  12. # If the block is "10S", steering is "S" and repeat is "10"
  13. steering=command_block[-1]
  14. repeat=command_block.to_i
  15. # We converse the steering instruction in a number
  16. steering_numerical=case(steering)
  17. when 'S'
  18. 0
  19. when 'L'
  20. -1
  21. when 'R'
  22. 1
  23. end
  24. # We add the instruction the required number of times in the steering array
  25. repeat.times{ steering_per_turn<< steering_numerical }
  26. end
  27. STDERR.puts "Commands to execute: #{steering_per_turn.to_s}"
  28.  
  29. # For each road pattern
  30. n.times do |i|
  31. qty,road = gets.chomp.split(';')
  32. # We execute the number of times the pattern is repeated
  33. qty.to_i.times do
  34. # New car position is previous position+new instruction
  35. car_pos += steering_per_turn.shift
  36. # Copy the road string
  37. line = road.dup
  38. # We place the car at the right position on the copied string
  39. line[car_pos] = '#'
  40. puts line
  41. end
  42. end
Add Comment
Please, Sign In to add comment