Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Detailed beginner solution ##
- # Number of road patterns
- n = gets.to_i
- steering_commands = gets.chomp.split(';')
- # Car position on the road first position is 1 and not 0
- # That's why we need to substract 1
- car_pos=steering_commands.shift.to_i-1
- # We will build an array of all the steering instructions to apply
- steering_per_turn=[]
- steering_commands.each do |command_block|
- # Steering instruction is the last letter of the command block
- # If the block is "10S", steering is "S" and repeat is "10"
- steering=command_block[-1]
- repeat=command_block.to_i
- # We converse the steering instruction in a number
- steering_numerical=case(steering)
- when 'S'
- 0
- when 'L'
- -1
- when 'R'
- 1
- end
- # We add the instruction the required number of times in the steering array
- repeat.times{ steering_per_turn<< steering_numerical }
- end
- STDERR.puts "Commands to execute: #{steering_per_turn.to_s}"
- # For each road pattern
- n.times do |i|
- qty,road = gets.chomp.split(';')
- # We execute the number of times the pattern is repeated
- qty.to_i.times do
- # New car position is previous position+new instruction
- car_pos += steering_per_turn.shift
- # Copy the road string
- line = road.dup
- # We place the car at the right position on the copied string
- line[car_pos] = '#'
- puts line
- end
- end
Add Comment
Please, Sign In to add comment