CodinGameのMars Lander – Level 1(PUZZLES:4問目)をRubyで解いてみた

LINEで送る
Pocket

CodinGameはこちら ゲームでプログラミング学習する英語のサイト

着々と4問目。考えすぎると難しい。英語が難しいのか、プログラミングが難しいのか。いや、その両方だなw。一旦英語で読んで、プログラミング書いてみて、わかんなかったらGoogleの翻訳で理解を補足していこう。そんな感じで進めていってます。

今回のミッション

火星に宇宙船を着陸させるゲーム。今回はレベル1ということで、まっすぐ着陸すればいいみたい。自由落下で重力加速度が3.711m/sあるため、4のパワーがないと位置を保てない。

着陸に必要な条件は以下のとおり

  • 平坦な地面に着地すること
  • 宇宙船の角度はまっすぐにすること(チルト角0度)
  • 垂直方向のスピードが40m/s以下
  • 水平方向のスピードが20m/s以下

必要なパラメータは、座標:XY、 水平のスピード:hSpeed、垂直のスピード:vSpeed、燃料:fuel、回転角度:rotate、推力:power。

出力するのは、rotateとpowerの2つ。それぞれ、前回よりも1段階しか変化しない点に注意。rotateは今の値±15度、powerは今の値±1まで。ただし、指定するときには最大値を指定することができる。例えば、powerが0のときでも4を指定することができるが、次のターンでは+1にしかならない。

今回書いたcode
STDOUT.sync = true # DO NOT REMOVE
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.

@surface_n = gets.to_i # the number of points used to draw the surface of Mars.
@surface_n.times do
    # land_x: X coordinate of a surface point. (0 to 6999)
    # land_y: Y coordinate of a surface point. By linking all the points together in a sequential fashion, you form the surface of Mars.
    land_x, land_y = gets.split(" ").collect {|x| x.to_i}
end

# game loop
loop do
    # h_speed: the horizontal speed (in m/s), can be negative.
    # v_speed: the vertical speed (in m/s), can be negative.
    # fuel: the quantity of remaining fuel in liters.
    # rotate: the rotation angle in degrees (-90 to 90).
    # power: the thrust power (0 to 4).
    x, y, h_speed, v_speed, fuel, rotate, power = gets.split(" ").collect {|x| x.to_i}
    
    # Write an action using puts
    # To debug: STDERR.puts "Debug messages..."
    
    STDERR.puts "x      :" + x.to_s
    STDERR.puts "y      :" + y.to_s
    STDERR.puts "h_speed:" + h_speed.to_s
    STDERR.puts "v_speed:" + v_speed.to_s
    STDERR.puts "fuel   :" + fuel.to_s
    STDERR.puts "rotate :" + rotate.to_s
    STDERR.puts "power  :" + power.to_s
    output_rotate = 0
    output_power = 0
    
    assumption_speed = 0 #until power = 4
    for i in (power..4)
        assumption_speed += (i - 3.711)
    end
    STDERR.puts "assumption_speed  :" + assumption_speed.to_s

    if v_speed < (-40  - assumption_speed + (3.711 - 3))
        output_power = 4
    else
        output_power = 0
    end
    puts output_rotate.to_s + " " + output_power.to_s
end
今回の設問についての感想など

今回は2回書いてみました。初めに書いたものが深く考えすぎて書いた結果、できたコードが意味不明のものになってしまったので、もう一度挑戦。再度前のコードを見直したときに、何を書いているのがかわからないことが判明w。あとで見てわかりやすいプログラムを書ことが重要だと理解しながらも、まだまだそんなレベルではないと実感(T ^ T)。

今回も他の例を幾つかみてみましたが、単純すぎてダメな例が多いという印象を受けました。今回の問題だけに対応している内容です。高さが違ったら墜落するだろうなって感じです。レベル1の問題だけなので、これでいいんだろうな。ただ、いいものが評価されて上位にくるはずが、どれも評価されていないので、いいものがどれかわからないのが残念だけど、気にせずに次へ進んでいこう!

LINEで送る
Pocket

カテゴリー: ruby

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です