fork download
  1. STDOUT.sync = true # DO NOT REMOVE!
  2.  
  3. # Average of all values.
  4. # @note Result has same type as sum; integer result is truncated.
  5. def mean(*args)
  6. args.sum / args.size
  7. end
  8.  
  9. # Upper-left and lower-right corners bounding the current search
  10. # area within the building.
  11. # @note Intervals are half-open for each axis: [x0, x1), [y0, y1)
  12. x0, y0 = 0, 0
  13. x1, y1 = gets.split.map(&:to_i)
  14. # Maximum number of turns before game over.
  15. n = gets.to_i
  16. # Current position of Batman.
  17. px, py = gets.split.map(&:to_i)
  18.  
  19. # Game loop.
  20. n.times do
  21. # The direction of the bomb from Batman's current position
  22. # (U, UR, R, DR, D, DL, L or UL).
  23. bomb_dir = gets.chomp
  24. # Move Batman closer to the bomb and reduce the search area.
  25. # @note Intercardinal directions are handled as a series of
  26. # cardinal directions.
  27. bomb_dir.each_char do |dir|
  28. case dir
  29. when 'U'
  30. py, y1 = mean(y0, py), py
  31. when 'D'
  32. py, y0 = mean(py, y1), py+1
  33. when 'L'
  34. px, x1 = mean(x0, px), px
  35. when 'R'
  36. px, x0 = mean(px, x1), px+1
  37. end
  38. end
  39. # Emit Batman's updated position.
  40. puts "#{px} #{py}"
  41. end
Success #stdin #stdout 0.01s 7960KB
stdin
10 10
3
0 0
DR
U
L
stdout
5 5
5 3
3 3