Created
June 11, 2018 12:50
-
-
Save martin-denizet/94afaa6b825080904c5a571128d47942 to your computer and use it in GitHub Desktop.
Codingame: Self-driving car testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment